Blogger Information
Blog 94
fans 0
comment 0
visits 92511
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
【Thinkphp】Thinkphp 常用数据库操作
可乐随笔
Original
1487 people have browsed it

Thinkphp 常用数据库操作

数据库操作官方文档:

https://www.kancloud.cn/manual/thinkphp6_0/1037533

1.查询数据

// ! 1.单条数据查询 : find()        // * 1.1 table方法必须指定完整的数据表名        // $users = Db::table('user')->find(4);        // $users = Db::table('user')->where('id', 1)->find();        // $users = Db::table('user')->where('name', '老李')->find();        // $users = Db::table('user')->where('id','>',3)->find();        //         // * 1.2 数组条件        // $users = Db::table('user')->where(        //     [        //         'name' => '老马',        //         'id' => 6        //     ]        // )->find();        // * 1.3 链式条件        // $users = Db::table('user')        // ->where('id','>',6)        // ->where('name','=', '老马')        // ->find();        // print_r($users);        // ? 更多查询表达式,参见官方手册:https://www.kancloud.cn/manual/thinkphp6_0/1037537        // ! 2.多条数据查询 select()        // $users = Db::table('user')->select();        // $users = Db::table('user')->where('id', '>', 3)->select();        //* 2.1 查询结果转数组        // $users = Db::table('user')->where('id', '>', 3)->select()->toArray();        // * 2.2 返回指定字段 field        // $users = Db::table('user')->field('id,name')->where('id', '>', 3)->select()->toArray();        // $users = Db::table('user')->field('id as uid,name')->where('id', '>', 3)->select()->toArray();        // $users = Db::table('user')->where('name', '老马')->select();        // * 2.3 查询某字段,返回一维数组        // $users = Db::table('user')->where('id','>',3)->column('name');        // * 2.4 查询某个值,返回字符串        // $users = Db::table('user')->where('id','=',3)->value('name');        // print_r($users);        // * 2.6 将数据库中某行复制到新列        // $users = Db::table('user')->select();        // foreach($users as $user)        // {        //     $rst = Db::table('user')->where('id', $user['id'])->update(['nickname' => $user['name']]);        // }        // print_r($rst);        // * 2.7 查询条件 '=' , '!=' , '<>' , '>' , '<' , 'like'        // $users = Db::table('user')->where('name','like','%张%')->select();        // $users = Db::table('user')->where('name','like','_张_')->select();        // print_r($users);        // * 2.8 区间查询 between && not between        // $users = Db::table('user')->where('id','between','8,12')->select();        // print_r($users);        // * 2.9 指定查询 in        // $users = Db::table('user')->where('id','in','5,8,9')->select();        // print_r($users);        // * 2.10 table() 包含表前缀,name()不包含表前缀,但需要在数据库配置中,配置前缀        // $adv = Db::table('adver')->order('sort desc,id desc')->select();        // print_r($adv);        // * 2.11 翻页\获取多少条,limit(),1个参数,表示获取多少条,2个参数表示,从第一个数字开始,获取多少条        // $shops = Db::table('shop')->limit(10,10)->order('id')->select()->toArray();        // ? limit方法需要计算页码起始数,不如page()方法方便        // $shops = Db::table('shop')->page(4,10)->order('id')->select()->toArray();        // $shops = Db::table('shop')->page(4)->limit(10)->order('id')->select()->toArray();        // print_r($shops);        // * 2.12 聚合查询        // $count = Db::table('user')->count();        // $count = Db::table('shop')->count('id');        // $count = Db::table('shop')->max('price');        // $count = Db::table('shop')->where('price','>',2000)->max('sales');        // * 查询已完成订单实际付款总金额        $count = Db::table('order')->where('status','=',9)->sum('sj_price');        print_r($count);

2.添加数据

// ! 3. 添加数据        // $data = [        //     'name' => '老张2',        //     'sex' => 1,        //     'age' => 30,        //     'phone' => '13902233333'        // ];        // 成功:返回 1 ,失败不返回        // $result = Db::table('user')->insert($data);        // if ($result) {        //     return '添加成功';        // } else {        //     return '添加失败';        // }        // 添加成功后,返回 user 主键 (这里返回:id)        // $id = Db::table('user')->insertGetId($data);        // if (empty($id)) {        //     return '添加失败';        // } else {        //     print_r($id);        // }

3.修改数据

// ! 4. 修改数据,限定条件        // $data = [        //     'name' => '小丽',        //     'sex' => 0,        //     'age' => 30,        //     'phone' => '13902233333'        // ];        // $update = Db::table('user')->where('id', '=', 1)->update($data);        // * 修改单独的值,用自增 inc +         // $update = Db::table('user')->where('id', 1)->inc('loginNum', 1)->update();

4.删除数据

// ! 5. 删除 Delete , 删除必须增加条件 , 不要用删除功能        // $result = Db::table('user')->where('id', '=', 2)->delete();        // if ($result) {        //     return '删除成功';        // } else {        //     return '删除失败';        // }        // * 软删除 useSoftDelete        // $result = Db::table('user')->where('id',5)->useSoftDelete('status',99)->delete();        // print_r($result);

5. 数据集操作

 // ! 6. 数据集操作        // * 删除数据集最后一条数据,返回被删除的数据        // $users = Db::table('user')->select();        // print_r($users->pop());        // * 指定字段排序        // $users = Db::table('user')->select();        // print_r($users);        // $usersAge = $users->order('age');        // print_r($usersAge);        // * 判断是否为空        // $news = Db::table('news')->select();        // if($news->isEmpty())        // {        //     return '数据集为空';        // } else {        //     print_r($news);        // }

6. 调试语句

// * getLastSql() : 打印原生SQL,执行SQL后再打印        // $user = Db::table('order')        //     ->where('status', '>=',2)        //     ->sum('sj_price');        // print_r($user);        // echo Db::getLastSql();        // * fetchSql() : 执行过程中打印原生SQL        // $user = Db::table('order')        //     ->where('status', '>=',2)        //     ->fetchSql()        //     ->sum('sj_price');        // print_r($user);

7. 多数据库操作

// * connect() : 方法,切换多数据库        // $order = Db::table('order')->select();        // $order = Db::connect('tpshop')->table('order')->select();        // print_r($order);

8. 数据库事务

// * startTrans 事务开启        // ? 事务要求 数据库引擎是: InnoDB, 不支持MyISAM        Db::startTrans();        $data = [            'ooid' => '1234567890',            'uid' => 1,            'price' => 111,            'phone' => '18809511111',            'name' => '欧阳克',            'sj_price' => 111,        ];        $oid = Db::table('order')->insertGetID($data);        // * 事务提交: commit ; 事务回滚: rollback        if (empty($oid)) {            Db::rollback();        }        // 订单联动提交,失败进行事物回滚        $details = Db::table('order_details')->insert([            'oid' => $oid,            'shop_id' => 1,            'price' => 111        ]);        $details = 0;        if (empty($details)) {            Db::rollback();        }        Db::commit();
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post