thinkphp5.0學習筆記(四)資料庫的操作
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是沒有區別的。


※js中的數組對象排序(方法sort()詳細介紹)
※簡訊發送介面被惡意訪問的網路攻擊事件(三)定位惡意IP的日誌分析腳本
※Akka(8): 分布式運算:Remoting-遠程查找式
※CNN壓縮:為反向傳播添加mask
TAG:達人科技 |
※thinkphp3.2跨資料庫聯合操作的簡單使用
※thinkphp5連接oracle資料庫
※renren-security 3.2.0 發布:支持主流資料庫
※華為nova 5 Pro現身GeekBench資料庫:妥妥麒麟980水準
※華為nova 5i現身Geekbench資料庫 搭載麒麟710
※歐亞資料庫曝光Apple Watch 4
※SQL_Server2000示例資料庫NorthWind的分析(轉)
※【資料】各種手的姿勢 (BY hong14cafe)
※ZenFone 5 Max現身Geekbench資料庫
※Galaxy Note 9現身Geekbench資料庫
※疑似小米9現身GeekBench資料庫;2019款iPhone曝光外觀巨變
※Python學習資料匯總(上)
※資料庫學習計劃——learning plan
※2018 年 StackOverflow 資料庫調查:PgSQL 排第三
※2019年NoSQL 資料庫 TOP 15:MongoDB、微軟、Couchbase、AWS、谷歌、Redis Labs
※價值680的Python學習課程資料免費領
※使用Django連接Oracle 11g資料庫
※php 工程師執行 redis keys×導致資料庫宕機,造成損失 400 萬元
※SpringBoot使用H2內嵌資料庫
※真的是8核8線程 i7-9700K現身SiSoftware資料庫