用接口实现封装的一个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; } } ?>

 

posted @ 2016-05-10 22:58  意灵魔法馆  阅读(472)  评论(0编辑  收藏  举报