数据表操作类

<?php  
  /*
  * 自动化数据表操作类 
  * @author 小蔡   <cyy0523xc@gmail.com> 
  * @version 1.0 
  
*/  
  class cls_crud extends cls_database  
 {  
      /*
      * 数据表名 
      * @var string 
      
*/  
      private $table =  '';  
      /*
      * 构造函数 
      * @param array $config     配置变量 
      
*/  
      public function __construct($config)  
     {  
          if(!emptyempty($config))  
         {  
              foreach($config  as $cf => $val)  
             {  
                 $ this->$cf = $val;  
             }  
         }  
         parent::__construct();  
     }  
      /*
      * 设置数据表(例如:在操作的过程中需要改变数据表,就可以使用此方法) 
      * @param string $table 
      
*/  
      public function set_table($table)  
     {  
         $ this->table = $table;  
     }  
      /*
      * 读取一条记录 
      * @param string $id        主键 
      * @param string $fields    获取字段 
      * @return array 
      
*/  
      public function read($id, $fields= ' * ')  
     {  
         $sql =  " SELECT {$fields} FROM `{$this->table}` WHERE `id`='{$id}' ";  
         $ this->query($sql);  
          return $ this->fetch_one();  
     }  
      /*
      * 插入一条记录 
      * @param array $array  数组 
      * @return bool 
      
*/  
      public function insert($array)  
     {  
         $fields = array();  
         $values = array();  
          foreach($array  as $f => $v)  
         {  
             $fields[] =  " `{$f}` ";  
             $values[] =  " ' ".mysql_real_escape_string($v). " ' ";  
         }  
         $fields = implode( ' , ', $fields);  
         $values = implode( ' , ', $values);  
         $sql =  " INSERT INTO `{$this->table}`({$fields}) VALUES({$values}) ";  
          return $ this->query($sql);  
     }  
      /*
      * 更新一条记录 
      * @param int   $id     主键 
      * @param array $array  数据数组 
      
*/  
      public function update($id, $array)  
     {  
        $values = array();  
          foreach($array  as $f => $v)  
         {  
             $values[] =  " `{$f}`=' ".mysql_real_escape_string($v). " ' ";  
         }  
         $values = implode( ' , ', $values);  
         $sql =  " UPDATE `{$this->table}` SET {$values} WHERE id='{$id}' limit 1 ";  
          return $ this->query($sql);  
     }  
      /*
      * 删除一条记录 
      * @param int $id   主键 
      * @return bool 
      
*/  
      public function delete($id)  
     {  
         $sql =  " DELETE FROM `{$this->table}` WHERE id='{$id}' limit 1 ";  
          return $ this->query($sql);  
     }  
      /*
      * 获取分页列表的数据 
      * @param array  $wheres    where条件数组,如果下标是数字,则直接加入条件,否则组合成:`{下标}`='{值}'这样的条件。最后用and链接 
      * @param string $order     排序字段 
      * @param int    $desc      是否是降序 
      * @param int    $offset    偏移量 
      * @param int    $limit     读取记录数 
      * @param int    $return_total  是否返回满足条件的记录总数,默认为0,需要显示分页时可以设置为1. 
      * @return array 
      
*/  
      public function get_list($wheres, $order= '', $desc= 1, $offset= 0, $limit= 8, $return_total= 0)  
     {  
          // 处理where条件  
          if($wheres)  
         {  
             $ where = array();  
              foreach($wheres  as $f => $v)  
             {  
                  if(is_numeric($f))  
                 {  
                     $ where[] = $v;  
                 }  
                  else  
                 {  
                     $ where[] =  " `{$f}`=' ".mysql_real_escape_string($v). " ' ";  
                 }  
             }  
             $ where = implode( '  and  ', $ where);  
         }  
          else  
         {  
             $ where =  ' 1 ';  
         }  
          // 处理orderby  
          if(!emptyempty($order))  
         {  
             $order =  " order by `{$order}`  " . ( 1===$desc ?  ' desc ' :  ' asc ');  
         }  
         $sql =  " SELECT * FROM `{$this->table}` WHERE {$where} {$order} limit {$offset}, {$limit} ";  
         $ this->query($sql);  
         $data = $ this->fetch_all();  
          if($return_total)  
         {  
              // 返回记录总数(分页)  
             $sql =  " SELECT count(*) FROM `{$this->table}` WHERE {$where} ";  
             $ this->query($sql);  
             $total = $ this->fetch_one();  
              return array( ' total '=>current($total),  ' list '=>$data);  
         }  
          else  
         {  
              return $data;  
         }  
     }  
 }  

转载于:https://www.cnblogs.com/qhorse/p/4588005.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值