1. 程式人生 > >PHP之ThinkPHP框架(資料庫)

PHP之ThinkPHP框架(資料庫)

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 }

  

 


參考:

http://www.thinkphp.cn/