码迷,mamicode.com
首页 > 数据库 > 详细

php-基于面向对象的MySQL类

时间:2016-07-20 19:24:36      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:


class sqlHelper{

       private $conn;

       private $host = ‘localhost‘;

      private $user = ‘root‘;

      private $pwd = ‘root‘;

      private $db;

     public function __construct($idb){

        $this->db = $idb;

                    $this->conn = new mysqli($this->host,$this->user,$this->$this->pwd,$this->idb);

                    if($this->conn->connect_error){

                    $this->error($this->conn->connect_error); 

      }

      $this->conn->query(‘set names  utf8‘);

      }

  //针对select语句

  public function execute_dql($sql){

                   $res = $this->conn->query($sql) or $this->error(‘查询操作失败‘.$this->conn->error);

                   $this->free();

                  return $res;

   }

//针对insert、update、delect

public function execute_dml($sql){

                   $flag =  $this->conn->query($sql) or $this->error($this->conn->error);

                   if(!$flag){

            return 0 ;//操作失败

        }else{

            if($this->conn->afffected_rows > 0){

                  return 1;//操作成功

            }else{

                return 2;//操作失败没有行数受到影响

            }

      }

}

//针对多行记录

public function fetch_all($sql){

         $res = $this->conn->query($sql) or $this->error(‘查询操作失败‘.$this->conn->error);

                $arr = array();

                while($row = $res->fetch_assoc()){

          $arr[] = $row;  //及时释放资源

    }

            

             return $arr;

}
protected function error($err){
                    $log = ‘cur.log‘;
                     file_put_contens($log,$err,FILE_APPEND);
                     die($err);
}
}
                  

 

 

 

php-基于面向对象的MySQL类

标签:

原文地址:http://www.cnblogs.com/YangJieCheng/p/5689324.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!