21,887
社区成员
发帖
与我相关
我的任务
分享
<?php
class connection{
public $conId = null;
private $properties = array();
function __construct(){
$this->readConfig();
$db_host = $this->properties["db_host"];
$db_user = $this->properties["db_user"];
$db_password = $this->properties["db_password"];
$this->doConnection($db_host, $db_user, $db_password);
}
function __destruct(){
` mysql_close($this->conId);
}
function doConnection($db_host, $db_user, $db_password){
$this->conId = mysql_connect($db_host, $db_user, $db_password) or die('Cannot connect to db: ' . mysql_error());
mysql_select_db($db_name,$this->conId);
if($this->conId){
echo "连接成功";
}else{
echo "连接失败";
}
}
function readConfig(){
$file_handle = fopen("../web.config", "r");
while (!feof($file_handle)) {
$line = fgets($file_handle);
$tempArray = explode("=", $line);
for($i = 0;$i < count($tempArray);$i++){
$temp = $tempArray[$i];
$tempArray[$i] = trim($temp);
}
$property = $tempArray[0];
$value = $tempArray[1];
$tempArray = array($property=>$value);
$this->properties = array_merge($this->properties , $tempArray);
}
fclose($file_handle);
}
}
new connection();
?>