Correction status:qualified
Teacher's comments:
<?php namespace app\index\controller; use think\Db; class Query { //读操作返回的都是二维数组,没有就是空数组 //写操作返回的是备操作的技术数量 public function find() { //查询单挑记录 $res = Db::table('staff') ->field(['name'=>'姓名','sex'=>'性别','salary'=>'工资']) ->where('staff_id',10) ->find(); dump($res); } public function select() { //查询多条记录 $res = Db::table('staff') ->field(['name'=>'姓名','sex'=>'性别','salary'=>'工资','staff_id'=>'id']) ->where('salary > 5000') ->order('salary DESC')//排序,从大到小 ->limit(2) ->select(); dump($res); } public function insert() { //新增单挑记录 // $data = [ // 'name'=>'9529', // 'sex'=>0, // 'age'=>22, // 'salary'=> 4568 // ]; // $num = Db::table('staff')->insert($data); // $id = Db::getLastInsID(); // return $num ?'添加成功,id='.$id :'没记录'; // $id = Db::table('staff')->insertGetId($data); // return $id ?'添加成功,id='.$id :'没记录'; // // $num = Db::table('staff')->($data)->insert(); // $id = Db::getLastInsID(); // return $num ?'添加成功,id='.$id :'没记录'; $data = [ ['name'=>'9529','sex'=>0,'age'=>22,'salary'=> 784512], ['name'=>'9530','sex'=>0,'age'=>22,'salary'=> 74125], ['name'=>'9531','sex'=>0,'age'=>22,'salary'=> 85268], ['name'=>'9532','sex'=>0,'age'=>22,'salary'=> 85214] ]; $num = Db::table('staff')->data($data)->insertAll(); return $num? '添加成功'.$num.'个记录':'no'; } public function updata() { //更新操作必须是基于前置查询,不允许无条件更新 //判断工资大于9999的下降1万元 // $num = Db::table('staff') // ->where('salary','>',9999) // ->data(['salary' => Db::raw('salary-10000')]) // ->update(); $num = Db::table('staff') ->update(['sex'=>1,'staff_id'=>26]); return $num? '更新成功'.$num.'个记录':'no'; } public function delete() { //删除是基于前置查询,不允许无条件删除 $num = Db::table('staff') ->where('salary','>',10000) ->delete(); //$num = Db::table('staff')->delete(ture);//清空表 return $num? '删除成功'.$num.'个记录':'no'; } }