Thinkphp5数据库的基本操作
一、数据库配置
配置文件 database.php
二、query execute 原生态SQL语句增删改查
在控制器中 use think\Db;
然后再在改控制器的方法中调用如下示例
execute 执行的是修改类型的操作
query 执行的是查询类型的操作
// 插入记录 // $result = Db::execute('insert into tp_data (id, name ,status) values (1, "1111",1)'); // dump($result); // 更新记录 // $result = Db::execute('update tp_data set name = "framework" where id = 1 '); // dump($result); // 查询数据 // $result = Db::query('select * from tp_data where id = 1'); // print_r($result); // 删除数据 //$result = Db::execute('delete from tp_data where id = 5 '); //dump($result); //其它操作 // 显示数据库列表 // $result = Db::query('show tables from tpshop1'); // print_r($result); // 清空数据表 //$result = Db::execute('TRUNCATE table tp_data'); // print_r($result);
三、参数绑定 命名占位符绑定(mysql预处理)
防止SQL注入
//参数绑定 // Db::execute('insert into tp_data (id, name ,status) values (?, ?, ?)', [3, 'thinkphp', 1]); // $result = Db::query('select * from tp_data where id = ?', [3]); // print_r($result); //命名占位符绑定 // Db::execute('insert into tp_data (id, name , status) values (:id, :name, :status)', ['id' => 11, 'name' => 'thinkphp', 'status' => 1]); // $result = Db::query('select * from tp_data where id=:id', ['id' => 10]); // print_r($result);
四、多个数据库切换查询操作
首先在config.php配置文件中 添加如下配置
// 数据库配置1 'db2' => [ // 数据库类型 'type' => 'mysql', // 服务器地址 'hostname' => '127.0.0.1', // 数据库名 'database' => 'tpshop2', // 数据库用户名 'username' => 'root', // 数据库密码 'password' => '', // 数据库连接端口 'hostport' => '', // 数据库连接参数 'params' => [], // 数据库编码默认采用utf8 'charset' => 'utf8', // 数据库表前缀 'prefix' => 'tp_', ], // 数据库配置2 'db3' => [ // 数据库类型 'type' => 'mysql', // 服务器地址 'hostname' => '127.0.0.1', // 数据库名 'database' => 'tpshop3', // 数据库用户名 'username' => 'root', // 数据库密码 'password' => '', // 数据库连接端口 'hostport' => '', // 数据库连接参数 'params' => [], // 数据库编码默认采用utf8 'charset' => 'utf8', // 数据库表前缀 'prefix' => 'tp_', ],
然后再控制器文件中 use think\Db;
在控制器的方法中添加如下代码
$result = Db::connect('db2')->query('select * from tp_data where id = 1'); print_r($result); $result = Db::connect('db3')->query('select * from tp_data where id = 1'); print_r($result); // $db1 = Db::connect('db1'); // $db2 = Db::connect('db2'); // $db1->query('select * from tp_data where id = 1'); // $db2->query('select * from tp_data where id = 1');
五、查询构造器
什么是查询构造器:
Db::name(‘不含表前缀的表名’) //推荐使用
Db::table(‘含表前缀的表名’)
// 插入记录 // Db::table('tp_data')->insert(['id' => 6, 'name' => 'thinkphp', 'status' => 1]); // 更新记录 // Db::table('tp_data') // ->where('id', 2) // ->update(['name' => "hello"]); // 查询数据 // $list = Db::table('tp_data') // ->where('id', 18) // ->select(); // 删除数据 // Db::table('tp_data') // ->where('id', 18) // ->delete(); // 插入记录 // Db::name('data')->insert(['id' => 7, 'name' => '77777777777777']);
六、DB链式操作
//链式操作 // 查询十个满足条件的数据 并按照id倒序排列 // $list = Db::name('data') // ->where('status', 1) // ->field('id,name') // ->order('id', 'desc') // ->limit(10) // ->select(); // print_r($list);
七、事务支持
//事务支持 在Mysql数据库中请设置表类型为InnoDB //把需要执行的事务操作封装到闭包里面即可自动完成事务 // Db::transaction(function () { // Db::table('tp_data')->delete(2); // Db::table('tp_data')->insert(['id' => 9, 'name' => 'thinkphp', 'status' => 1]); // }); // 手动控制事务的提交 // 启动事务 // Db::startTrans(); // try { // Db::table('tp_data') // ->delete(2); // Db::table('tp_data') // ->insert(['id' => 11, 'name' => 'thinkphp', 'status' => 1]); // // 提交事务 // echo 'try'; // Db::commit(); //此处需要提交 不提交的话就进入回滚操作 // } catch (\Exception $e) { // // 回滚事务 // echo 'catch'; // Db::rollback(); // }