用接口實現封裝的一個mysqli工具類

<?php
   
   class DAOMysqli  implements I_Dao{

     //本類的對象實例
     private static $instance;
     //結果集
     private $result_row;
     //配置信息
     private $_host;
     private $_root;
     private $_dbname;
     private $_pw;
     private $_port;
     private $_charset;
    
    //mysqli實例化對象
     private $_mysqli;
     private function __construct($option){
       $this->_initArray($option);
       $this->_initMysqli();
     }
     private function __clone(){
     
     }
     //定義一個單例模式
     public static function getSingleton(array $option=array()){
      if(!(self::$instance instanceof self)){
         self::$instance =new self($option);
      }
         return self::$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(){ //實例化mysqli對象 $this->_mysqli=new MYSQLI($this->_host,$this->_root,$this->_pw,$this->_dbname,$this->_port); if($this->_mysqli->connect_errno){ echo "鏈接失敗".$this->_mysqli->connect_error; exit; } //設置字符集編碼 $this->_mysqli->set_charset=$this->_charset; } //用於查詢的方法 public function query($sql=''){ $result=$this->_mysqli->query($sql); if(false==$result){ trigger_error("執行失敗,你的sql語句有問題".$sql."錯誤消息".$this->_mysqli->error); return false; } $this->result_row=$result; return $result; } //用於非查詢的方法 public function execu($sql=''){ $result=$this->query($sql); if(false==$result){ return false; }else{ echo "執行成功"; } } //查詢全部的記錄 public function fetchAll($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; } //查詢一條記錄 public function fetchRow($sql=''){ $result=$this->query($sql); if(false==$result){ return false; } $row=$result->fetch_array(MYSQLI_ASSOC); $result->free(); return $row?$row:false; } //查詢某條記錄第一個字段 public function fetchOne($sql=''){ $result=$this->query($sql); if(false==$result){ return false; } $row=$result->fetch_array(MYSQLI_NUM); $result->free(); return $row?$row[0]:false; } //查詢某個字段的全部記錄 public function fCoulumn($sql,$coulumn){ $result=$this->fetchAll($sql); if(false==$result){ return false; } $rows=array(); foreach($result as $row){ $rows[]=$row[$coulumn]; } return $rows; } //用於提供轉義的方法 public function escapeData($data=''){ } //獲取被影響的記錄數 public function affectedRow(){ $affected_row=$this->_mysqli->affected_rows; if($affected_row==0){ echo "操做成功,但沒有被影響"; } return $affected_row; } //獲取結果影響的記錄數 public function resultRow(){ $num_rows=$this->result_row->num_rows; $this->result=null; return $num_rows; } //獲取最新自動生成的ID public function lastInsertId(){ return $this->_mysqli->insert_id; } } ?>
相關文章
相關標籤/搜索