return [ // 數據庫類型 type = mysql , // 數據庫連接DSN配置 dsn = , // 服務器地址 hostname = 127.0.0.1 , // 數據庫名 database = thinkphp , // 數據庫用戶名 username = root , // 數據庫密碼 password = , // 數據庫連接端口 hostport = , // 數據庫連接參數 params = [], // 數據庫編碼默認采用utf8 charset = utf8 , // 數據庫表前綴 prefix = think_ , // 數據庫調試模式 debug = false, // 數據庫部署方式:0 集中式(單一服務器),1 分布式(主從服務器) deploy = 0, // 數據庫讀寫是否分離 主從式有效 rw_separate = false, // 讀寫分離后 主服務器數量 master_num = 1, // 指定從服務器序號 slave_no = , // 是否嚴格檢查字段是否存在 fields_strict = true, ];
2、字符串方式:
Db::connect( mysql://root:[email protected]:3306/thinkphp#utf8 數據庫類型://用戶名:密碼@數據庫地址:數據庫端口/數據庫名#字符集
二、query(查詢操作)execute(寫入操作) 原生態SQL語句 增刪改查
Db::execute( insert into t_test(username,password) html' target='_blank'>values( qqq , qqq ) Db::execute( update t_test set username = 55 where id = 10 Db::query( select * from t_test where id = 5 Db::execute( delete from t_test where id = 6
三 參數綁定 命名占位符綁定
支持參數綁定: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 = Db::execute( insert into think_user (id, name) values (:id, :name) ,[ id = 8, name = thinkphp
四、查詢構造器
1、查詢數據:
(1)查詢一個數據使用:
// table方法必須指定完整的數據表名Db::table( think_user )- where( id ,1)- find();//find 方法查詢結果不存在,返回 null
(2)查詢數據集
Db::table( think_user )- where( status ,1)- select();select 方法查詢結果不存在,返回空數組
如果設置了數據表前綴參數的話,可以使用
Db::name( user )- where( id ,1)- find();Db::name( user )- where( status ,1)- select();
如果你的數據表沒有使用表前綴功能,那么name和table方法的一樣的效果。
(3)助手函數
系統提供了一個db助手函數,可以更方便的查詢:
db( user )- where( id ,1)- find();db( user )- where( status ,1)- select();
2、添加數據:
(1)添加一條數據
$data = [ foo = bar , bar = foo Db::table( think_user )- insert($data);
(2)添加多條數據
$data = [ [ foo = bar , bar = foo ], [ foo = bar1 , bar = foo1 ], [ foo = bar2 , bar = foo2 ]];Db::name( user )- insertAll($data);insertAll 方法添加數據成功返回添加成功的條數
(3)助手函數
// 添加單條數據db( user )- insert($data);// 添加多條數據db( user )- insertAll($list);
3、更新數據:
(1)更新數據表中的數據
Db::table( think_user ) - where( id , 1) - update([ name = thinkphp
(2)助手函數
// 更新數據表中的數據db( user )- where( id ,1)- update([ name = thinkphp // 更新某個字段的值db( user )- where( id ,1)- setField( name , thinkphp // 自增 score 字段db( user )- where( id , 1)- setInc( score // 自減 score 字段db( user )- where( id , 1)- setDec( score
4、刪除數據
(1)刪除數據表中// 根據主鍵刪除
Db::table(‘think_user’)- delete(1);
Db::table(‘think_user’)- delete([1,2,3]);
// 條件刪除
Db::table(‘think_user’)- where(‘id’,1)- delete();
Db::table(‘think_user’)- where(‘id’,’ ’,10)- delete();助手函數
// 根據主鍵刪除db( user )- delete(1);// 條件刪除 db( user )- where( id ,1)- delete();
五、鏈式操作
假如我們現在要查詢一個User表的滿足狀態為1的前10條記錄,并希望按照用戶的創建時間排序 ,代碼如下:
Db::table( think_user ) - where( status ,1) - order( create_time ) - limit(10) - select();
這里的where、order和limit方法就被稱之為鏈式操作方法,除了select方法必須放到最后一個外(因為select方法并不是鏈式操作方法),鏈式操作的方法調用順序沒有先后。
本文講解關于ThinkPHP5數據庫的相關操作 ,更多相關內容請關注php 。
相關推薦:
關于ThinkPHP5的數據庫和模型用法
關于thinkphp5.0數據庫操作的案例
列舉ThinkPHP5與ThinkPHP3的一些異同點
以上就是關于ThinkPHP5數據庫的相關操作的詳細內容,PHP教程
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答