代码示例如下:有不好的地方希望大家讨论、留言。
<?php namespace app\home\model; use think\Controller; use think\Db; /** * MYSQL模型 */ class Mysql extends controller { //获取一条数据 public function getFind($table,$where=array(),$field='',$order='id desc'){ $result=Db::name($table)->where($where)->field($field)->order($order)->find(); return $result; } //更新数据 public function getUpdate($table,$where=array(),$params){ $result=Db::name($table)->where($where)->update($params); return $result; } //插入一条数据返回id public function getInsertGetId($table,$params){ $result=Db::name($table)->insertGetId($params); return $result; } //查询多条数据 public function getSelect($table,$where=array(),$field='',$order='id desc'){ $result=Db::name($table)->where($where)->order($order)->field($field)->select(); return $result; } //查询有限制数据 public function getLimitSelect($table,$where=array(),$field='',$order='id desc',$limit){ $result=Db::name($table)->where($where)->order($order)->field($field)->limit($limit)->select(); return $result; } //数据分页 public function getPageSelect($table,$where=array(),$page=0,$pageSize=10,$order='id desc',$field=''){ $result=Db::name($table)->where($where)->order($order)->field($field)->page($page,$pageSize)->select(); return $result; } //删除数据 public function getDelete($table,$where=array()){ $result=Db::name($table)->where($where)->delete(); return $result; } //插入多条数据 public function getInsertAll($table,$data){ $result=Db::name($table)->insertAll($data); return $result; } //两表连接查询 public function TwoSelectJoin($table1,$table2,$condition,$where=array(),$field='',$order=array('t1.id desc')){ $result=Db::name($table1.' t1')->join($table2.' t2',$condition)->where($where)->order($order)->field($field)->select(); return $result; } //两表连接查询 public function TwoFindJoin($table1,$table2,$condition,$where=array(),$field='',$order=array('t1.id desc')){ $result=Db::name($table1.' t1')->join($table2.' t2',$condition)->where($where)->order($order)->field($field)->find(); return $result; } //三表连接查询 public function ThreeSelectJoin($table1,$table2,$table3,$condition1,$condition2,$where=array(),$field='',$order=array('t1.id desc')){ $result=Db::name($table1.' t1')->join($table2.' t2',$condition1)->join($table3.' t3',$condition2)->where($where)->order($order)->field($field)->select(); return $result; } //三表连接查询 public function ThreeFindJoin($table1,$table2,$table3,$condition1,$condition2,$where=array(),$field='',$order=array('t1.id desc')){ $result=Db::name($table1.' t1')->join($table2.' t2',$condition1)->join($table3.' t3',$condition2)->where($where)->order($order)->field($field)->find(); return $result; } //统计某表某字段 public function getSum($table,$where=array(),$field){ $result=Db::name($table)->where($where)->sum($field); return $result; } //统计某表记录数 public function getCount($table,$where=array()){ $result=Db::name($table)->where($where)->count(); return $result; } //查询某列查询 public function getColumn($table,$where=array(),$field='',$order='id desc'){ $result=Db::name($table)->where($where)->order($order)->column($field); return $result; } //查询单个字段值 public function getValue($table,$where=array(),$value){ $result=Db::name($table)->where($where)->value($value); return $result; } //查询某个字段 public function getOrderValue($table,$where=array(),$value){ $result=Db::name($table)->where($where)->value($value); return $result; } //查询锁定表(队列,阻止并发) public function getLockValue($table,$where=array(),$value,$order='id desc'){ $result=Db::name($table)->where($where)->lock(true)->value($value); return $result; } }使用说明: