An MYSQLI tool class for encapsulating with interfaces

Source: Internet
Author: User

<?PHPclassDaomysqliImplementsi_dao{//object instances of this class     Private Static $instance; //result set     Private $result _row; //configuration Information     Private $_host; Private $_root; Private $_dbname; Private $_PW; Private $_port; Private $_charset; //mysqli instantiating an object     Private $_mysqli; Private function__construct ($option){       $this->_initarray ($option); $this-_initmysqli (); }     Private function__clone () {}//define a singleton pattern      Public Static functionGetsingleton (Array $option=Array()){      if(! (Self::$instanceinstanceof Self)) { Self::$instance=NewSelf$option); }         returnSelf::$instance; }     Private function_initarray ($option){       $this->_host=isset($option[' Host ']?$option[' Host ']: '; $this->_root=isset($option[' Root ']?$option[' Root ']: '; $this->_dbname=isset($option[' dbname '])?$option[' dbname ']: '; $this->_pw=isset($option[' PW '])?$option[' PW ']: '; $this->_port=isset($option[' Port ']?$option[' Port ']: '; $this->_charset=isset($option[' CharSet '])?$option[' CharSet ']: ';
} Private function_initmysqli () {//instantiating a Mysqli object $this->_mysqli=NewMysqli ($this->_host,$this->_root,$this-&GT;_PW,$this->_dbname,$this-_port); if($this->_mysqli->Connect_errno) { Echo"Connection failed."$this->_mysqli->Connect_error; Exit; } //setting Character Set encoding $this->_mysqli->set_charset=$this-_charset; } //Methods for querying Public functionQuery$sql= ' '){ $result=$this->_mysqli->query ($sql); if(false==$result){ Trigger_error("Execution failed, your SQL statement has a problem.")$sql." Error message ".$this->_mysqli->error); return false; } $this->result_row=$result; return $result; } //methods for non-queries Public functionExecu ($sql= ' '){ $result=$this->query ($sql); if(false==$result){ return false; }Else{ Echo"Successful Execution"; } } //querying all the records Public functionFetchall ($sql= ' '){ $result=$this->query ($sql); if(false==$result){ return false; } $rows=Array(); while($row=$result-Fetch_array (MYSQLI_ASSOC)) { $rows[]=$row; } $result-Free (); return $rows; } //Query a record Public functionFetchrow ($sql= ' '){ $result=$this->query ($sql); if(false==$result){ return false; } $row=$result-Fetch_array (MYSQLI_ASSOC); $result-Free (); return $row?$row:false; } //Query the first field of a record Public functionFetchone ($sql= ' '){ $result=$this->query ($sql); if(false==$result){ return false; } $row=$result-Fetch_array (Mysqli_num); $result-Free (); return $row?$row[0]:false; } //query all records for a field Public functionFcoulumn ($sql,$coulumn){ $result=$this->fetchall ($sql); if(false==$result){ return false; } $rows=Array(); foreach($result as $row){ $rows[]=$row[$coulumn]; } return $rows; } //method to provide escape Public functionEscapedata ($data= ' '){ } //gets the number of records affected Public functionAffectedrow () {$affected _row=$this->_mysqli->affected_rows; if($affected _row==0){ Echo"Operation succeeded, but not affected"; } return $affected _row; } //gets the number of records affected by the result Public functionResultrow () {$num _rows=$this->result_row->num_rows; $this->result=NULL; return $num _rows; } //get the latest auto-generated IDs Public functionLastinsertid () {return $this->_mysqli->insert_id; } }?>

An MYSQLI tool class for encapsulating with interfaces

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.