1. 程式人生 > >ThinkPHP5學習(8)資料庫-基本操作

ThinkPHP5學習(8)資料庫-基本操作

1.資料庫配置

2.query,execute原生態sql語句,增刪改查

3.引數繫結命名佔位符繫結

4.多個數據切換查詢操作

5.查詢構造器

6.DB鏈式操作

7.事務支援

ThinkPHP內建了抽象資料庫訪問層,把不同的資料庫操作封裝起來,我們只需要使用公共的Db類進行操作。

資料庫配置。

application/database.php

每個模組可以設定獨立的資料庫連線引數,admin/database.php

可以採用字串方式,動態定義連線資訊

如database.php中已經定義好,

可以寫:

也可以在模型裡單獨設定資料庫連線資訊

建立一個表:

CREATETABLE `tp_user` (

`id`int(11) NOT NULL,

`name`varchar(45) DEFAULT NULL,

`status`tinyint(1) DEFAULT NULL,

`score`tinyint(2) DEFAULT NULL,

`email`varchar(45) DEFAULT NULL,

PRIMARYKEY (`id`)

)ENGINE=InnoDB DEFAULT CHARSET=utf8;

出現:

Class'app\admin\controller\Db' not found

則:

use think\Db;

  1. <?php

  2. // 資料庫

  3. namespace app\admin\controller;

  4. use think\Db;

  5. class index2

  6. {

  7. // 查詢資料

  8. public function test1(){

  9. // 基本使用

  10. // 插入記錄

  11. Db::execute('insert into tp_user (id,name) values(?,?)',[8,'thinkphp']);

  12. // 更新記錄

  13. Db::execute('update tp_user set name="thinkphp" where id=1 ');

  14. // 查詢資料

  15. Db::query('select * from tp_user where id=?',[8]);

  16. // 刪除資料

  17. Db::query('delete from tp_user where id=5');

  18. // 顯示資料列表

  19. Db::query('show tables from tp5');

  20. // 清空資料表

  21. Db::execute('TRUNCATE table tp_user');

  22. // 命名佔位符

  23. Db::query('select * from tp_user where id=:id',[1]);

  24. Db::execute('insert into tp_user (id,name) values(:id,:name)',[8,'thinkphp']);

  25. // 查詢構造器

  26. // 查詢一個數據

  27. Db::table('tp_user')->where('id',1)->find();

  28. // 找不到返回null

  29. // 查詢資料集

  30. Db::table('tp_user')->where('status',1)->select();

  31. // 找不到返回空陣列

  32. // 設定了表字首時

  33. Db::name('user')->where('id',1)->find();

  34. Db::name('user')->where('status',1)->select();

  35. // find,select 方法之前可以使用所有的鏈式操作方法

  36. // db助手函式

  37. db('user')->where('id',1)->find();

  38. // 使用db助手函式預設每次重新連線資料庫,而使用Db::name,或Db::table都是單例的

  39. // 如需要採用相同的連結,可以傳入第三個引數。第二個引數為資料庫的連線引數

  40. db('user',[],false)->where('id',1)->find();

  41. // 使用query物件或閉包查詢

  42. $query = new \think\db\Query();

  43. $query->table('tp_user')->where('status',1);

  44. Db::find($query);

  45. // Db::select($query);

  46. Db::select(function($query){

  47. $query->table('tp_user')->where('status',1);

  48. });

  49. // 返回某個欄位值,

  50. Db::table('tp_user')->where('id',1)->value('name');

  51. // 查詢某一列的值

  52. Db::table('tp_user')->where('status',1)->column('name');

  53. // 指定索引

  54. Db::table('tp_user')->where('status',1)->column('name','id');

  55. // 資料集批量處理

  56. Db::table('tp_user')->chunk(100,function($users){

  57. foreach($users as $user){

  58. //

  59. echo '1';

  60. }

  61. });

  62. // 或者交給回撥方法myUserIterator處理

  63. Db::table('tp_user')->chunk(100,'myUserIterator');

  64. // 你可以通過從閉包函式中返回false來中止對資料集的處理

  65. Db::table('tp_user')->chunk(100,function($users){

  66. // 處理結果集

  67. return false;

  68. });

  69. // 也支援在 chunk 方法之前呼叫其它的查詢方法

  70. Db::table('tp_user')->where('score','>',80)->chunk(100,function($users){

  71. foreach($users as $user){

  72. //

  73. }

  74. });

  75. // JSON型別資料查詢

  76. // 查詢JSON型別欄位,info欄位為json型別

  77. Db::table('tp_user')->where('info$.email','[email protected]')->find();

  78. }

  79. // 新增資料

  80. public function test2(){

  81. // 5版本,新增 data/inc/dec/exp方法設定資料

  82. $data = ['foo'=>'bar','bar'=>'foo'];

  83. Db::table('tp_user')->insert($data);

  84. Db::name('user')->insert($data);

  85. // insert 方法新增資料成功返回新增的條數,正常情況返回1

  86. Db::name('user')->insert($data);

  87. $userId = Db::name('user')->getLastInsID();

  88. // 或者直接使用inserGetId方法新增資料並返回主鍵值

  89. Db::name('user')->insertGetId($data);

  90. // 新增多條資料

  91. $data = [

  92. ['foo'=>'bar','bar'=>'foo'],

  93. ['foo'=>'bar1','bar'=>'foo1'],

  94. ['foo'=>'bar2','bar'=>'foo2'],

  95. ];

  96. Db::name('user')->insertAll($data);

  97. // 助手函式

  98. db('user')->insert($data);

  99. db('user')->insertAll($data);

  100. // 快捷更新

  101. Db::table('user')

  102. ->data(['name'=>'tp','score'=>90])

  103. ->insert();

  104. }

  105. // 更新資料

  106. public function test3(){

  107. Db::table('tp_user')

  108. ->where('id',1)

  109. ->update(['name'=>'thinkphp']);

  110. // 資料中包含主鍵,直接使用

  111. Db::table('tp_user')

  112. ->update(['name'=>'thinkphp','id'=>1]);

  113. // update方法返回影響資料的條數,沒修改任何資料返回0

  114. // 使用Sql函式

  115. Db::table('tp_user')

  116. ->where('id',1)

  117. ->update([

  118. 'login_time'=>['exp','now()'],

  119. 'login_times'=>['exp','login_times+1']

  120. ]

  121. );

  122. // 更新某個欄位的值

  123. Db::table('tp_user')

  124. ->where('id',1)

  125. ->setField('name','thinkphp');

  126. // 自增自減

  127. // setInc/setDec

  128. Db::table('tp_user')

  129. ->where('id',1)

  130. ->setInc('score');

  131. Db::table('tp_user')

  132. ->where('id',1)

  133. ->setInc('score',5);

  134. Db::table('tp_user')

  135. ->where('id',1)

  136. ->setDec('score');

  137. Db::table('tp_user')

  138. ->where('id',1)

  139. ->setDec('score',5);

  140. // 延遲更新

  141. Db::table('tp_user')->where('id',1)->setInc('score',1,10);

  142. // 助手函式

  143. db('user')->where('id',1)->update(['name'=>'thinkphp']);

  144. db('user')->where('id',1)->setField('name','thinkphp');

  145. db('user')->where('id',1)->setInc('score');

  146. db('user')->where('id',1)->setDec('score');

  147. // 快捷更新

  148. Db::table('tp_user')

  149. ->where('id',1)

  150. ->inc('read')

  151. ->dec('score',3)

  152. ->exp('name','UPPER(name)')

  153. ->update();

  154. }

  155. }