实例
<?php namespace app\index\controller; use think\Db; class Query { // 单条查询 public function find() { $res = Db::table('user')->field('id,username')->where('id','>',2)->find(); dump($res); } // 多条查询 public function select() { $res = Db::table('user') ->field(['id'=>'序号','username'=>'姓名','jb'=>'金币']) ->where('jb','>',1000) ->order('id','desc') ->limit(2) ->select(); dump($res); } // 单条插入 public function insert() { $data = ['username'=>'msn99303','password'=>md5('mdn8399')]; // $res_num = Db::table('user')->insert($data); // $res_id = Db::getLastInsID(); // return $res_id ? '成功添加,id='.$res_id : '没成功'; // data($data):讲要处理的数据打包 $option[] // insertGetId() == insert() + getLastInsID() $res_id = Db::table('user')->data($data)->insert(); return $res_id ? '成功添加,id='.$res_id : '没成功'; } // 多条插入 public function insertall() { $data[] = ['username'=>'mnd3hf2','password'=>md5('n399922'),'jb'=>7945654]; $data[] = ['username'=>'d3ddvc','password'=>md5('2312312'),'jb'=>5453]; $data[] = ['username'=>'2322ddf','password'=>md5('111111'),'jb'=>1234245]; $num = Db::table('user')->data($data)->insertAll(); return $num ? '添加成功'.$num.'条记录':'添加失败'; } // 更新操作 public function update() { $num = Db::table('user')->where('jb','<=',30200)->data(['jb'=>Db::raw('jb+1000')])->update(); return $num ? '更新成功'.$num.'条记录':'更新失败'; } // 删除操作 public function delete() { $num = Db::table('user')->where('id','>',10)->delete(); return $num ? '删除成功'.$num.'条记录':'删除失败'; } }
运行实例 »
点击 "运行实例" 按钮查看在线实例