基本使用
配置了數據庫連接信息后,我們就可以直接使用數據庫運行原生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是一個單獨的數據庫配置,支持數組和字符串,也可以是一個數據庫連接的配置參數名。
文檔最后更新時間:2018-04-26 09:25:41
未解決你的問題?請到「問答社區」反饋你遇到的問題