框架数据连接类

class db{
     /**
      * 定义数据库连接
      */
    /**
     * return 返回一个值叫做链式调用
     *每执行完一个方法就会返回当前对象
     *返回的对象 可以调用类中的成员变量和成员方法
     */
    protected $mysqli; //连接工具
     
    protected $table;  //数据表
    
    protected $opt;  //存储条件
    
     function  __construct($table_name){  //定义一个数据库连接
          
            $this->config($table_name);
        
    }
     protected function config($table_name){
         
            $this->mysqli=new mysqli(HOST, USER, PWD, DBBASE);
            $this->table=$table_name;  //表名
            if(mysqli_connect_errno()){
                
                  echo "数据库连接错误".mysqli_connect_erron();
                  exit();
                
            }
            $this->mysqli->query("set names 'latin1'");
            $this->opt['filed']="*"; //设置默认查询字段
            $this->opt['where']=$this->opt['limit']=$this->opt['order']=$this->opt['group']=' '; //设置他们的条件都为空
          }
          
          /**
           * 获取表的字段
           */
          function fileds(){
                $result=$this->mysqli->query("DESC{$this->table}"); //查询表结构
                $filedsarray=array();
                while ($rows=$result->fetch_assoc()!=false){ //循环字段关联数组
                                    $filedsarray[]=$rows['Field'];
                    
      }
                    return $filedsarray;
      }
      /**
       * 查询表的字段 
       * 
       */
      function filed($filed){
            /*首先$filed就是字段 查询的字段 首先判断获取的字段是否是字符串
             * 
             */
          if (is_string($filed)){
              
               $filedarry=explode(",", $filed);  /*分割字符串*/
          }else{
                $filedarry=$filed;
                  }
          if(is_array($filedarry)){
              $filed="";
              foreach ($filedarry as $v){
                  $filed .= "`" . $v . "`" . ",";
              }
           }
           return rtrim($filed,",");
  }
  /**
   * 条件方法where
   */
  function where($where){
       if(is_string($where)){
             $this->opt['where']="WHERE".$where;
       }else{
           $this->opt['where']="";
       }
       return $this;
   }
   /**
    * limit 条件
    */
   
   function limit($limit){
       if(is_string($limit)){
           $this->opt['limit']="LIMIT".$limit;
       }else{
           $this->opt['limit']="";
           
       }
       return $this;
   }
   /**
    * order by 条件
    */
   function order($order){
         if (is_string($order)){
             $this->opt['order']="ORDER BY".$order;
         }else{
             $this->opt['order']="";
         }
         return $this;
    }
    
    /**
     * group by 调教
    */
    function group($group){
        if (is_string($group)){
            $this->opt['group']="GROUP BY".$group;
        }else{
            $this->opt['group']="";
        }
        return $this;
    }
      /**
       * select语句查询
       */
    function select(){
        /**
         * $sql=select * from 表名 where limiy order by group by
         */
        $sql="SELECT {$this->opt['filed']} FROM {$this->table} {$this->opt['where']}{$this->opt['order']}{$this->opt['group']}{$this->opt['limit']}";
        
        return $this->sql($sql);
     }
     /**
      * delete 删除语句
      */
     function delate($id=""){
         if($id==""&&empty($this->opt['where'])) die ("获取的查询条件不能为空");
         if($id!=""){
            if(is_array($id)){
                 $id=implode(",",$id);/*implode 将获取的数组分割成字符串 每分割一个加个逗号*/
             }
             $this->opt['where']="WHERE id IN(".$id.")";
         }
         /*delete from 表名 where 条件*/
         $sql="DELETE FROM {$this->table} {$this->opt['where']} {$this->opt['limit']}";
         echo $sql."<br/>";
         return $this->query($sql);
      }
      /**
       * 返回sql语句结果集
       */
    function sql($sql) {
        $result = $this->mysqli
        ->query ( $sql ) or die ( $this->dbError () );
        $resultArr = array ();
        while ( ($row = $result->fetch_assoc ()) != false ) {
            $resultArr [] = $row;
        }
        return $resultArr;
    }
      /**
       * insert语句
       */
      function insert($ins){
          is_array($ins) or die("非法数组");
          $fileds=$this->filed(array_keys($ins));
     var_dump($fileds);
          $values=$this->values(array_values($ins));
          var_dump($values);
          $sql="INSERT INTO {$this->table}(".$fileds.") VALUES(".$values.")";
          if($this->query($sql)>0){
              return $this->mysqli->insert_id;
          }
      }
      /**
       * 将数组转换字符串并进行转义
       */
      protected function values($value){
          if (!get_magic_quotes_gpc()){
              $str="";
              foreach ($value as $v){
                  $str.="'".addslashes($v)."',";
              }
          }else{
              foreach ($value as $v){
              $str.="'.$v.',";
              }
          }
          return rtrim($str,",");
      }
      /**
       * 没有结果集
       */
      function query($sql){
          $this->mysqli->query($sql)or die($this->error());
          return $this->mysqli->affected_rows; //返回前一个 Mysql 操作的受影响行数
      }
      /**
       * update 更新语句
       * update biao set 列明=新值 where 列明=某值
       */
      function update($up){
          is_array($sql)or die("非法字符串");
          if (empty($this->opt['where'])) die("条件不能为空");
          $set="";
          $gpc=get_magic_quotes_gpc();
          while (list($k,$v)=each($up)){
                  $v = ! $gpc ? addslashes ( $v ) : $v;
            $set .= "`{$k}`='" . $v . "',";
          }
          $set=rtrim($set,",");
          $sql="UPDATE {$this->table} SET $set {$this->opt['where']}";
          return $this->query($sql);              
  }
  /**
   * 查寻总记录记录数
   */
  function count($tablename=""){
      $tabname = $tabname == '' ? $this->table : $tabname;
      $sql = "SELECT `id` FROM {$tabname} {$this->opt['where']}";
      return $this->query ( $sql );
  }
    /**
     * 查询单机路数
     */
  function find($id){
      $sql = "SELECT {$this->opt['field']} FROM {$this->table}    WHERE `id` = {$id}";
      return $this->sql ( $sql );
  }
  /**
   * 返回错误
   */
  function error(){
      return $this->mysqli->error;
  }
    
}
原文地址:https://www.cnblogs.com/mengluo/p/4795885.html