Database operation class based on mysqli

Source: Internet
Author: User
Tags rekey

<?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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.