摘要:<?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; } //獲取數(shù)據(jù)條數(shù) 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; } //查詢分頁數(shù)據(jù) 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()]; } } ?>
批改老師:查無此人批改時間:2019-02-27 09:07:10
老師總結(jié):完成的不錯。where條件,最好多加點(diǎn)判斷,能剔除數(shù)據(jù)庫注入。繼續(xù)加油