PHP是網站后台開發語言,其重要的操作對象莫過於數據庫,之前有了解過mysqli和pdo,但ThinkPHP的數據庫交互必須使用其特定的封裝方法,或者可以認為其是對PHP數據庫操作的進一步封裝,以達到更加安全和高效。
ThinkPHP內置了抽象數據庫訪問層,把不同的數據庫操作封裝起來,我們只需要使用公共的Db類進行操作,而無需針對不同的數據庫寫不同的代碼和底層實現,Db類會自動調用相應的數據庫驅動來處理。采用PDO方式,目前包含了Mysql、SqlServer、PgSQL、Sqlite等數據庫的支持。
數據庫連接配置
在APP目錄下的database.php中進行相關參數的配置
測試准備工作
建立數據表tb_test,並在database.php中配置('prefix' => 'tb_',)使表前綴為tb_,然后建立模板Test.php,控制器Testx.php
控制器Testx.php實現
1 <?php 2 //其中user為模塊名,需對應更改 3 namespace app\user\controller; 4 5 //導入建立的模板,其中user為模塊名,Test為模板類 6 use app\user\model\Test; 7 8 9 class Testx 10 { 11 public function show() 12 { 13 $test=new Test();//得到模板的操作對象 14 $res=$test->testshow();//調用模板中的方法 15 dump($res);//輸出 16 17 } 18 }
SQL語句數據庫操作
使用execute()和query()方法實現數據庫操作,是直接使用SQL原生語句進行數據表操作
execute可實現數據表的增、刪、改,操作后會返回影響行數
query可實現數據表的增、刪、改、查,只有查詢會返回結果
模板Test.php實現:
1 <?php 2 //其中user為模塊名,需對應更改 3 namespace app\user\model; 4 use think\Model; 5 class Test extends Model 6 { 7 8 public function testshow(){ 9 10 //---------execute的使用---------- 11 //插入方法1 12 //返回操作行數1 13 //$res=Test::execute("insert into tb_test values(1,'dong1','dong11','dong111')"); 14 15 //插入方法2 16 //返回操作行數1 17 //$res=Test::execute("insert into tb_test values(?,?,?,?)",[9,"W2","X1","N1"]); 18 19 20 //-----------query的使用---------- 21 //查詢 22 //返回查詢結果數組 23 //$res=Test::query("select * from tb_test"); 24 25 //插入 26 //無結果返回 27 $res=Test::query("insert into tb_test values(?,?,?,?)",[19,"W12","X12","N12"]); 28 29 return $res; 30 31 } 32 }
基於PDO的添加語句
模板Test.php實現:
1 <?php 2 //其中user為模塊名,需對應更改 3 namespace app\user\model; 4 use think\Model; 5 class Test extends Model 6 { 7 8 public function testshow(){ 9 10 //返回影響行數1 11 //$daa = ['text1' => '東小東', 'text2' => '100']; 12 //$res=Test::insert($daa); 13 14 15 //得到最后一次操作的id 16 //$res= Test::getLastInsID(); 17 18 19 //一次性插入多條數據 20 $indata=[ 21 ['text1' => '東小東1', 'text2' => '100'], 22 ['text1' => '東小東2', 'text2' => '100'], 23 ['text1' => '東小東3', 'text2' => '100'], 24 ]; 25 //返回影響行數3 26 $res=Test::insertAll($indata); 27 28 return $res; 29 30 } 31 }
基於PDO的刪除語句
模板Test.php實現:
1 <?php 2 //其中user為模塊名,需對應更改 3 namespace app\user\model; 4 use think\Model; 5 class Test extends Model 6 { 7 8 public function testshow(){ 9 10 //返回影響行數 11 12 //條件為id=3 13 //$res=Test::where('id',3)->delete(); 14 15 //條件為id>30 16 $res=Test::where('id','>',30)->delete(); 17 18 return $res; 29 20 } 21 }
基於PDO的修改語句
模板Test.php實現:
1 <?php 2 //其中user為模塊名,需對應更改 3 namespace app\user\model; 4 use think\Model; 5 class Test extends Model 6 { 7 8 public function testshow(){ 9 10 //返回影響行數,如果修改的值和原來相同,則返回0 11 12 //更新 13 //$res=Test::where('id', 9)->update(['text3' => '99999']); 14 15 16 //自增2 17 //$res=Test::where('text1', "東小東1")->setInc('id', 2); 18 19 //自減2 20 $res=Test::where('text1', "dongxiaodong")->setDec('id', 2); 21 22 return $res; 23 24 } 25 }
基於PDO的查詢語句
模板Test.php實現:
1 <?php 2 //其中user為模塊名,需對應更改 3 namespace app\user\model; 4 use think\Model; 5 class Test extends Model 6 { 7 8 public function testshow(){ 9 10 11 //查詢數據庫表的全部信息 12 //返回二維數組,結果不存在返回空數組 13 //$res=Test::select(); 14 15 //指定條件查詢 16 //$res=Test::where("text1","dong1")->select(); 17 18 19 //查詢符合條件的第一條數據,結果不存在返回null 20 //返回一維數組 21 //$res=Test::where("text1","W2")->find(); 22 23 24 //查詢某個字段的值,只能查詢到第一個,失敗返回空 25 //$res=Test::where("text1","dong1")->value('text2'); 26 27 28 //查詢某個字段的值,查詢所有,失敗返回空數組 29 //$res=Test::where("text1","W12")->column('text3'); 30 31 32 //模糊查詢 like,不區分大小寫,返回數組 33 $res=Test::where("text1","like","%W%")->select(); 34 35 //區間查詢,返回數組 36 //$res=Test::where("id","between",[2,7])->select(); 37 38 39 //統計行數 40 echo count($res); 41 42 //打印單個數據 43 //echo $res[0]["text1"]; 44 45 //find方法打印 46 //echo $res["text1"]; 47 48 49 return $res; 50 51 52 //每次處理2條數據,適用於對大數據處理 53 /* 54 Test::chunk(2, function($users) { 55 56 foreach ($users as $user) { 57 dump($user); 58 echo "------------"; 59 //功能 60 } 61 echo "********************"; 62 63 }); 64 */ 65 } 66 }
Where和whileOr條件的補充
1 <?php 2 //其中user為模塊名,需對應更改 3 namespace app\user\model; 4 use think\Model; 5 class Test extends Model 6 { 7 8 public function testshow(){ 9 10 //and 兩個字段分別對應的兩個條件必須同時成立 11 /* 12 $res=Test::where("id",6) 13 ->where("text1","like","%W%") 14 ->select(); 15 */ 16 17 //多字段的and & 兩個字段對應的同一個條件必須同時成立 18 /* 19 $res=Test::where("text1&text3","like","%W%") 20 ->select(); 21 */ 22 23 //or whereOr 兩個條件其中一個成立即可 24 /* 25 $res=Test::where("id",8) 26 ->whereOr("text1","like","%W%") 27 ->select(); 28 */ 29 30 31 //多字段的or 兩個字段如果其中一個滿足共同條件即可 32 $res=Test::where("text1|text3","like","%W%") 33 ->select(); 34 35 return $res; 36 37 } 38 }
其他細節補充
1 //獲取到表的字段,類型,主鍵等信息,返回數組 2 $res=Test::getTableInfo(); 3 4 $res=Test::where("id",12) 5 ->order("id") //排序,添加實參desc為降序,默認asc 6 ->limit(0,10)//獲取第[0,10]條數據 7 //->page("3,10") //分頁,參數1為當前頁,參數2為每頁的條數 8 ->field("id,text3") //提取的字段 9 ->whereOr("text1","like","%W%") //以where共用表示其中一個條件滿足即可 10 ->select();
Db模塊操作數據庫
以上是使用模板繼承Model來實現數據庫操作的,當然還有另一種數據庫的操作方法,那就是使用系統的Db模塊。如果使用該模塊則必須先use think\Db;
1 <?php 2 //其中user為模塊名,需對應更改 3 namespace app\user\model; 4 use think\Db; 5 6 class Test 7 { 8 9 public function testshow(){ 10 11 //插入 12 //$daa = ['text1' => '東小東', 'text2' => '222222']; 13 //name則表示使用配置好的表前綴 14 //$res=Db::name('test')->insert($daa); 15 16 17 //查詢1 18 //table則需要加上表的前綴 19 //$res=Db::table("tb_test")->select(); 20 21 //查詢2 22 $res=Db::query("select * from tb_test"); 23 24 return $res; 25 26 } 27 }
參考: