<?PHP/***************************** Database Access Class (Mysqli version) ******************************/classmysqlidb{//Database link itself Private $link; //the last SQL statement executed Private $sql; //Initialize class Public function__construct ($hostname,$username,$password,$database){ $this->link=NewMysqli ($hostname,$username,$password,$database); if(Mysqli_connect_error()){ $errorInfo= ' error:could not make a database link ('.Mysqli_connect_errno().‘)‘.Mysqli_connect_error(); Throw NewErrorexception ($errorInfo, 1); } $this->link->set_charset ("UTF8"); } //gets the ID that was generated after the insert Private functionGetlastid () {return $this->link->insert_id; } //gets the number of rows affected Private functioncountaffected () {return $this->link->affected_rows; } //assemble the conditions of an array format into a string Private functionRewhere ($where){ $str= ' '; foreach($where as $key=$value){ if(!Empty($str)){ $str. = ' and '; } $rekey=$this->link->escape_string ($key); $revalue=$this->link->escape_string ($value); $str.=Is_numeric($revalue)?" `$rekey`=$revalue":"`$rekey=$revalue‘"; } return $str; } Public functionCreate$table,$model){ $fields= ' '; $values= ' '; foreach($model as $key=$value) { if($fields){ $fields. = ', '; } $fields.="`$key`"; if($values){ $values. = ', '; } $values.=Is_numeric($value)?$value:"‘".$this->link->escape_string ($value)."‘"; } $this->sql= "INSERT into"$table`($fields) VALUES ($values)"; Echo $this-SQL; $this->link->query ($this-SQL); return $this->link->insert_id; } Public functionModify$table,$model,$where){ $assins= ' '; $where=$this->rewhere ($where); foreach($model as $key=$value) { $rkey=$this->link->escape_string ($key); $rvalue=$this->link->escape_string ($value); if(!Is_numeric($rvalue)){ $rvalue= "'".$rvalue."‘"; } $assins.=$assins?",`$rkey`=$rvalue":"`$rkey`=$rvalue"; } $this->sql= "UPDATE"$table' SET$assinsWHERE$where"; Echo $this-SQL; $this->link->query ($this-SQL); } Public functionRemove$table,$where){ $where=$this->rewhere ($where); $this->sql= "DELETE from"$table' WHERE$where"; Echo $this-SQL; $this->link->query ($this-SQL); } Public functionSearch$table,$where){ $where=$this->rewhere ($where); $this->sql= "SELECT * from"$table' WHERE$where"; $this->link->query ($this-SQL); $result=$this->link->query ($this-SQL); return $result-Fetch_assoc (); } Public functionUnique$table,$where){ $where=$this->rewhere ($where); $this->sql= "SELECT * from"$table' WHERE$whereLIMIT 1 "; $result=$this->link->query ($this-SQL); return $result-Fetch_object (); }}
Database operation class based on mysqli