• 数据表操作类


    <?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;  
             }  
         }  
     }  
  • 相关阅读:
    netcore 开发问题整理(图片地址)
    netcore 开发问题整理(下拉框)
    C# 请求网址中汉字的编码转换
    C# 时间戳
    c# 判断指定文件是否存在
    js数组常用方法整理
    服务器断电 Mysql启动失败
    Bean初始化之postProcessBeforeInitialization、afterPropertiesSet、init-method、postProcessAfterInitialization等方法的加载
    Java多线程基础
    Mysql多字段order by用法
  • 原文地址:https://www.cnblogs.com/qhorse/p/4588005.html
Copyright © 2020-2023  润新知