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