自己实现的数据库访问类

Original 2019-02-27 05:50:46 248
abstract:<?php namespace util; use think\Db; class SysDb {   //获取表   public function table($table)   {     $this->where =&n
<?php
namespace util;
use think\Db;
class SysDb
{
  //获取表
  public function table($table)
  {
    $this->where = '';
    $this->order = '';
    $this->limit = 0;
    $this->field = '*';
    $this->table = $table;
    return $this;
  }
  //获取字段
  public function field($field='*')
  {
    $this->field = $field;
    return $this;
  }
  //获取排序
  public function order($order='')
  {
    $this->order = $order;
    return $this;
  }
  //获取数据条数
  public function limit($limit = '')
  {
    $this->limit = $limit;
    return $this;
  }
  //获取查询条件
  public function where($where = [])
  {
    $this->where = $where;
    return $this;
  }
  //获取单条记录
  public function item()
  {
    return Db::name($this->table)->field($this->field)->where($this->where)->find();
  }
  //获取多条记录
  public function lists()
  {
    $query = Db::name($this->table)->field($this->field)->where($this->where);
    $this->order && $query->order($this->order);
    $this->limit && $query->limit($this->limit);
    return $query->select();
  }
  //查询指定索引
  public function cates($index)
  {
    $query = Db::name($this->table)->field($this->field)->where($this->where);
    $this->order && $query->order($this->order);
    $this->limit && $query->limit($this->limit);
    $res = $query->select();
    $data = [];
    foreach($res as $v){
      $data[$v[$index]] = $v;
    }
    return $data;
  }
  //查询分页数据
  public function pages($pageSize=10)
  {
    $total = Db::name($this->table)->where($this->where)->count();
    $query = Db::name($this->table)->field($this->field)->where($this->where);
    $this->order && $query->order($this->order);
    $pageObj = $query->paginate($pageSize,$total);
    return ['total'=>$total,'data'=>$pageObj->items(),'pages'=>$pageObj->render()];
  }
}
 ?>


Correcting teacher:查无此人Correction time:2019-02-27 09:07:10
Teacher's summary:完成的不错。where条件,最好多加点判断,能剔除数据库注入。继续加油

Release Notes

Popular Entries