基本使用
配置了数据库连接信息后,我们就可以直接使用数据库运行原生SQL操作了,支持query
(查询操作)和execute
(写入操作)方法,并且支持参数绑定。
Db::query("select * from think_user where id=?",[8]);
Db::execute("insert into think_user (id, name) values (?, ?)",[8,"thinkphp"]);
也支持命名占位符绑定,例如:
Db::query("select * from think_user where id=:id",["id"=>8]);
Db::execute("insert into think_user (id, name) values (:id, :name)",["id"=>8,"name"=>"thinkphp"]);
可以使用多个数据库连接,使用
Db::connect($config)->query("select * from think_user where id=:id",["id"=>8]);
$config是一个单独的数据库配置,支持数组和字符串,也可以是一个数据库连接的配置参数名。