基本使用

配置了數(shù)據(jù)庫(kù)連接信息后,我們就可以直接使用數(shù)據(jù)庫(kù)運(yùn)行原生SQL操作了,支持query(查詢操作)和execute(寫入操作)方法,并且支持參數(shù)綁定。

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']);

可以使用多個(gè)數(shù)據(jù)庫(kù)連接,使用

Db::connect($config)->query('select * from think_user where id=:id',['id'=>8]);

$config是一個(gè)單獨(dú)的數(shù)據(jù)庫(kù)配置,支持?jǐn)?shù)組和字符串,也可以是一個(gè)數(shù)據(jù)庫(kù)連接的配置參數(shù)名。

文檔最后更新時(shí)間:2018-04-26 09:25:41

文檔
目錄

深色
模式

切換
寬度