zoukankan      html  css  js  c++  java
  • 用接口实现封装的一个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; } } ?>
    一萧一剑走江湖,一笑一乐看世界,一切美好的事物我们都需要去用心感受,聆听自然给予我们的欢乐. 小时候觉得长大了多好,可以到外面的世界看一看,可长大了,却向往童年般的生活,没有烦恼,该哭的时候哭,该笑的时候笑,想做什么都可以.即便我们长大了更应该热爱生活懂得爱自己,不要抱怨生活对你的不公,开心的活着比什么都好。 所以在这个有限的时间里,我们需要快乐高兴的活着,活出属于自己的青春.忘记那些不痛快的事情.我们需要梦想来使得我们的人生更有意义. 今天就分享到这里吧,大家可以叫我一萧,可能在以后的日子里,我分享的不仅仅是代码上的事情,因为能让我们感到快乐和高兴并且获得收获的不仅仅是技术层面上的,应该有很多很多
  • 相关阅读:
    设计模式实战应用之五:工厂方法模式
    Codeforces445A_DZY Loves Chessboard(预处理)
    void f(int(&amp;p)[3]){} 和void f(int(*p)[3]){}的差别
    《linux 内核全然剖析》 mktime.c
    Java中对象、对象引用、堆、栈、值传递以及引用传递的详解
    android 仿ios开关控件
    ViewDragHelper实战 自己打造Drawerlayout
    [javase学习笔记]-8.5 statickeyword的使用场景
    玩转图片Base64编码
    Android stuido viewpagerindicator的使用
  • 原文地址:https://www.cnblogs.com/ylmfg/p/5479772.html
Copyright © 2011-2022 走看看