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

 

转载于:https://www.cnblogs.com/ylmfg/p/5479772.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值