日期:2014-05-17 浏览次数:20399 次
<?php
class DB{
private $hostname;
private $username;
private $password;
private $select_db;
private $con;
private $Error;
public function __construct($hostname,$username,$password,$select_db){
if(!empty($hostname)&&!empty($username)&&!empty($select_db))//检查参数是否为空,否则不赋值
{
$this->hostname = $hostname;
$this->username = $username;
$this->password = $password;
$this->select_db = $select_db;
$this->con = mysql_connect($this->hostname,$this->username,$this->password);
if(!$this->con){
$this->Error = die('Could Not Connect:'.mysql_error);
}
else{
mysql_select_db($this->select_db,$this->con);
}
}
}
public function __destruct(){//退出时结束连接
mysql_close($this->con);
}
public function insert($table,$body){//插入table中的一个数组
$line1 = implode(',',$body);
$line2 = implode(',',array_keys($body));
echo $sql = "INSERT INTO $table ($line2) VALUES ($line1)";
$result = mysql_query($sql,$this->con);
if(!$result){
echo $this->Error;
echo '111';
}
}
public function update($table,$body){
}
public function read($table,$keyword){
}
public function delete($table,$keyword){
}
public function getLastError(){//返回最后一条错误信息
return $this->Error;
}
}
?>
<?php
require('DB.class.php');
$DB = new DB('localhost','root','','dbtest');
$line1 = array(
'aa'=>'`aa`',
'bb'=>'`dd`'
);
$DB->insert('1234',$line1);
?>