Correction status:qualified
Teacher's comments:完成的不错。
8月13号作业:
thinkphp6:数据库增删查改语句、表达式语句
1.数据库原生的增删改查
//用query方式(原生方式)查询 $data = Db::query('select * from user '); echo '<pre>'.print_r($data,true); //原生方法插入 $execute = Db::execute("insert into user set `phone`='13211111111',`u_name`='一灯大师' "); $execute = Db::execute("insert into user set `phone` = '13233334445',`u_name` = '小飞侠'"); print_r($execute); //原生方法更新 $execute = Db::execute("update user set `u_name`='武三通' where `uid`=5 "); $execute = Db::execute("update user set `u_name` = '月牙儿', `phone` = '15643436653' where `uid` = 16"); print_r($execute);
点击 "运行实例" 按钮查看在线实例
2.用单链模式查询
//单链查询方法之单条数据查询,一般应用于只有一条数据的查询,比如超级管理员,当前会员等等 // $find = Db::table('user') ->where('uid',1) -> find(); // $find = Db::table('user') ->where('uid',1) ->find(); // print_r($find);
点击 "运行实例" 按钮查看在线实例
3.用select,insert,save,update,delete方法
//select方法查询,select 方法查询结果是一个二维数组,如果结果不存在,返回空数组 $select = Db::table('user') -> where('status',1) -> select(); print_r($select); // 查询某个字段的值 value ,value 方法查询结果不存在 $value = DB::table('user') ->where('status',1) -> value('u_name'); print_r($value); // 查询某一列的值 column ,column('a','b'),a代表值 ,b代表Key $column = DB::table('user') ->where('age',22) -> column('u_name','uid'); print_r($column); // insert 方法添加数据成功返回添加成功的条数,通常情况返回1 $insert = DB::table('user') ->insert(['u_name' => '花儿菲儿','age' => '16','sex' => '2','phone' => '13366662233']); $data = ['u_name' => '日月星辰','age' => '18','sex' => '2','phone' => '13366662233']; $insert = Db::table('user') ->insert($data); print_r($insert); //save 方法统一写入数据,自动判断是新增还是更新数据(以写入数据中是否存在主键数据为依据) //增加数据 $data = ['u_name' => '高山流水','age' => '20','sex' => '1','phone' => '13366662233']; $save = Db::table('user') ->save($data); //更新数据1 $data = ['u_name' => '高山流水','age' => '20','sex' => '1','phone' => '13366662233','uid' => '20']; $save = Db::table('user') ->save($data); //更新数据2 $data = ['u_name' => '高山流水','age' => '20','sex' => '1','phone' => '13366662233','uid' => '20']; $save = Db::table('user') ->update($data); //添加一条数据 insertGetId,添加数据成功返回添加数据的自增主键 $data = ['u_name' => '逆流成河','age' => '20','sex' => '1','phone' => '13366662233']; $insertGetId = Db::table('user') ->insertGetId($data); print_r($insertGetId); // 添加多条数据 insertAll,insertAll 方法添加数据成功返回添加成功的条数 $data = [ ['u_name' => '风中飘舞','age' => '18','sex' => '1','phone' => '13366663344'], ['u_name' => '花儿乱颤','age' => '16','sex' => '1','phone' => '13366665566'] ]; $insertAll = Db::table('user') ->insertAll($data); print_r($insertAll); //修改数据 update,可以使用where $data = ['u_name' => '枯叶蝴蝶','age' => '20','sex' => '1','phone' => '13366662233']; $update = Db::table('user') ->where('uid',13) ->update($data); print_r($update);
点击 "运行实例" 按钮查看在线实例
4.表达式方式
// = $select = Db::table('user') -> where('uid','=','2') -> select(); print_r($select); //<> $select = Db::table('user') -> where('uid','<>','2') -> select(); print_r($select); //>= $select = Db::table('user') -> where('uid','>=','12') -> select(); print_r($select); //< $select = Db::table('user') -> where('uid','<','9') -> select(); print_r($select); //<= $select = Db::table('user') -> where('uid','<=','8') -> select(); print_r($select); // 查询范围如下,like, wherelike, not like, whereNotLike, in,not in ,null, not null,whereNotIn, whereNotNull //like,%燕%为模糊查询,%代表着文字,相当于占位符 $select = Db::table('user') -> where('u_name','like','%燕%') -> select(); print_r($select); //wherelike使用同上,但是()内不再写like $select = Db::table('user') -> wherelike('u_name','%燕%') -> select(); print_r($select); //not like,%燕%为模糊查询,%代表着文字,相当于占位符 $select = Db::table('user') -> where('u_name','not like','%燕%') -> select(); print_r($select); //whereNotLike使用同上,但是()内不再写like $select = Db::table('user') -> whereNotLike('u_name','%燕%') -> select(); print_r($select); //between $select = Db::table('user') -> where('uid','between','1,2') -> select(); $select = Db::table('user') -> whereBetween('uid','1,2') -> select(); print_r($select); //not between $select = Db::table('user') -> where('uid','not between','1,5') -> select(); $select = Db::table('user') -> whereNotbetween('uid','1,5') -> select(); print_r($select); //in $select = Db::table('user') -> where('uid','in','1,5') -> select(); print_r($select); //not in $select = Db::table('user') -> where('uid','not in','1,5') -> select(); print_r($select);
点击 "运行实例" 按钮查看在线实例