ThinkPHP內建了抽象資料庫訪問層,把不同的資料庫操作封裝起來,我們只需要使用公用的Db類進行操作,而無需針對不同的資料庫寫不同的代碼和底層實現,Db類會自動調用相應的資料庫驅動來處理。採用PDO方式,目前包含了Mysql、SqlServer、PgSQL、Sqlite等資料庫的支援。
1.基本使用
配置了資料庫連接資訊後,我們就可以直接使用資料庫運行原生SQL操作了,支援query(查詢操作)和execute(寫入操作)方法,並且支援參數綁定。
public function read(){$sql = Db::query('select * from news'); dump($sql); }
輸出的是:
execute方法:
public function read(){$sql = Db::execute('insert into news (nid, rid) values (11, 11)');; dump($sql);}
輸出:
資料庫添加成功!
也支援命名預留位置綁定,例如:
public function read(){$sql = Db::query('select * from news where nid=:nid',['nid'=>1]); dump($sql);}
輸出:
execute方法:
public function read(){$sql = Db::execute('insert into news (nid, rid) values (:nid, :rid)',['nid'=>18,'rid'=>'121']); dump($sql);}
輸出:
資料庫添加成功!
可以使用多個資料庫連接:
2.查詢構造器
聽名字就知道,很裝X..
先來看基本查詢;
查詢一個資料:
// table方法必須指定完整的資料表名$sql =Db::table('news')->where('nid',1)->find(); dump($sql);
find = 查詢一條;並且查詢結果不存在,返回 null
輸出:
Db::table('think_user')->where('status',1)->select();
這條查詢語句與上面同效,但是select 方法查詢結果不存在,返回空數組
額 這個玩意叫查詢資料集,沒錯!
預設情況下,find和select方法返回的都是數組。
如果你要查詢某個欄位的值,咋整?
public function read(){// 返回某個欄位的值$sql =Db::table('news')->where('nid',18)->value('rid'); dump($sql);}
這樣看輸出,我求rid的值:
如果你需要處理成千上百條資料庫記錄,可以考慮使用chunk方法,該方法一次擷取結果集的一小塊,然後填充每一小塊資料到要處理的閉包,該方法在編寫處理大量資料庫記錄的時候非常有用。
public function read(){$sql =Db::table('news')->chunk(1,function($user){foreach($user as $u) {dump($u); } }); }
這個樣子 就可以一條一條都給遍曆出來了!
是“一條一條·”,嘿!
3.添加資料跟刪除資料
使用 Db 類的 insert 方法向資料庫提交資料
public function read(){$data = ['ntitle' => '123', 'rid' => '456'];$sql = Db::table('news')->insert($data); dump($sql);}
添加成功後insert 方法返回添加成功的條數,insert 正常情況返回 1
添加資料後如果需要返回新增資料的自增主鍵,可以使用getLastInsID方法:
public function read(){$data = ['ntitle' => '123', 'rid' => '345'];$sql = Db::table('news')->insert($data);$userId = Db::name('news')->getLastInsID('nid');dump($userId);dump($sql);}
看輸出:
主鍵欄位22!
添加多條資料:
添加多條資料直接向 Db 類的 insertAll 方法傳入需要添加的資料即可;
public function read(){$data = [['ntitle' =>'gaga','rid' => '12'],['ntitle' =>'gaaaga','rid' => '123']];$sql = Db::table('news')->insertAll($data);dump($sql);}
這樣的話,返回的應該是兩條2
刪除資料:
根據主鍵來刪除
public function read(){// 根據主鍵 來刪$sql = Db::table('news')->delete(1);//多刪//$sql = Db::table('news')->delete(1,2,3);dump($sql);}
執行成功返回影響行數;
還有一種是根據條件來刪除的
public function read(){// 根據條件 來刪$sql = Db::table('news')->where('nid',18)->delete();//多刪//$sql = Db::table('news')->where('nid','<',1)->delete();dump($sql);}
執行成功也是返回影響行數;
4.查詢方法:
where方法:
可以使用where方法進行AND條件查詢:
public function read(){$sql = Db::table('news')->where('nid',20)->where('ntitle',123)->find();dump($sql);}
whereOr方法:
使用whereOr方法進行OR查詢:
public function read(){$sql = Db::table('news')->where('nid',20)->whereOr('ntitle','like','%123%')->find();dump($sql);}
混合查詢:
where方法和whereOr方法在複雜的查詢條件中經常需要配合一起混合使用
public function read(){$sql = Db::table('news')->where(function($query){$query->where('nid',21)->where('nid',22);})->whereOr(function($query){$query->where('ntitle','123')->whereOr('ntitle','123');})->select();dump($sql);}
輸出的是:
看一下產生的程式碼:
SELECT * FROM `news` WHERE ( `nid` = 1 OR `nid` = 2 ) OR ( `ntitle` LIKE '123' OR `ntitle` LIKE '123' )
第一個查詢方法用where或者whereOr是沒有區別的。
查詢接五