最近写的DB类,这里留作存档

来源:互联网 发布:阿里云已经备案的域名 编辑:程序博客网 时间:2024/06/11 04:09
<?php/**********************************************************************************************************************************Author:MichaelssssDate:2013/4/13QQ:602254985Version:0.1.1未作传入参数正确性验证,请一定要确定自己传入的参数正确,未完成对或,非的条件判断请勿传入尽量用函数实现,若不符合要求,比如对条件的与或非,请自行用exec($sql)执行自己的sql语句Fix:exec语句可以正确执行现在默认写入数据库编码为UTF-8现在在数据库中查询不到东西是返回falseHowTo:$DB = new DB($hostname,$username,$password);三个参数必须不为空$array = array(插入的列=>需要插入的值);以下所有的传入数组都是相同,仅限用数组传入$DB->insert($table,$array);返回一个BOOLEN$DB->update($table,$ori,$keyword)ori是查询更新的原始条件keyword是要更新的键值返回BOOLEN$DB->find($table,$condition)$condition是查询的条件返回一个二维数组,若查询不成功或不存在,返回fasle$DB-delete($table,$condition)$condition是删除的条件,删除是将条件所在的一整行删除返回BOOLEN***********************************************************************************************************************************/class DB{private $hostname;private $username;private $password;private $select_db;private $con;private $Error;private $coding;/*****************************************************构造函数*****************************************************/public function __construct($hostname,$username,$password,$select_db,$coding='UTF8'){if(!empty($hostname)&&!empty($username)&&!empty($select_db))//检查参数是否为空,否则不赋值{$this->hostname = $hostname;$this->username = $username;$this->password = $password;$this->select_db = $select_db;$this->coding = $coding;$this->con = mysql_connect($this->hostname,$this->username,$this->password);mysql_query("SET NAMES $this->coding");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));$sql = "INSERT INTO `$table` ($line2) VALUES ('$line1')";$result = mysql_query($sql,$this->con);if($result){return true;}else{return false;}}/***********************************************************************************************************************//*****************************************************更新函数**********************************************************//***************ori是where后的条件,body是替换的东西**********************/public function update($table,$ori,$body){$sqlset="";foreach($body as $line2=>$line1){$sqlset = $sqlset."$line2=$line1,";}$sqlset = substr($sqlset,0,-1);$sqlwhere ="";foreach($ori as $ori_line2=>$ori_line1){$sqlwhere = $sqlwhere."AND"." $ori_line2='$ori_line1'";}$sql = "UPDATE `$table` SET $sqlset WHERE 1=1 $sqlwhere";$result = mysql_query($sql,$this->con);if(!$result){$this->Error = '更新出错'.mysql_error($this->con);return false;}else{return true;}}/************************************************************************************************************************//*****************************************************查询函数***********************************************************//****************** 返回一个二维数组 ******************/public function find($table,$col,$keyword){$sqlwhere ="";foreach($keyword as $line2 => $line1){$sqlwhere = $sqlwhere."AND"." $line2='$line1'";}if(!empty($col)){//如果有指定查询列,则设定,否则返回对应条件的一整行if(count($col)==1){$col1 = $col;}else{$col1 = implode(',',$col);}$sql = "select $col1 FROM `$table` where 1=1 $sqlwhere";$result = mysql_query($sql,$this->con);$tmp = array();if($result){while($row = mysql_fetch_array($result,MYSQL_ASSOC)){array_push($tmp,$row);}return $tmp;}else{$this->Error = '数据库连接出错'.mysql_error($this->con);return false;}}else{$sql = "select * FROM `$table` where 1=1 $sqlwhere";$result = mysql_query($sql,$this->con);$tmp = array();if($result){while($row = mysql_fetch_array($result,MYSQL_ASSOC)){array_push($tmp,$row);}return $tmp;}else{$this->Error = '数据库连接出错'.mysql_error($this->con);return false;}}}public function findAll($table){$sqlwhere = "";$sql = "SELECT * FROM `$table`";$result = mysql_query($sql,$this->con);$tmp = array();if($result){while($row = mysql_fetch_array($result,MYSQL_ASSOC)){array_push($tmp,$row);}return $tmp;}else{$this->Error = '数据库连接出错'.mysql_error($this->con);return false;}}/************************************************************************************************************************//***************************************************删除函数**************************************************************/public function delete($table,$keyword){$sqlwhere = "";foreach($keyword as $line2=>$line1){$sqlwhere = $sqlwhere."AND"." $line2='$line1'";}$sql = "DELETE FROM `$table` where 1=1 $sqlwhere";$result = mysql_query($sql,$this->con);if(!$result){$this->Error = '删除出错'.mysql_error($this->con);return false;}else{return true;}}/*************************************************************************************************************************/public function Exec($sql){return mysql_query($sql,$this->con);}public function setCoding($code){$this->coding = $code;}public function getLastError(){//返回最后一条错误信息return $this->Error;}}?>