配置了數據庫連接信息后,我們就可以直接使用數據庫運行原生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是一個單獨的數據庫配置,支持數組和字符串,也可以是一個數據庫連接的配置參數名。
點贊