PHP之ThinkPHP框架(数据库)

PHP是网站后台开发语言,其重要的操作对象莫过于数据库,之前有了解过mysqli和pdo,但ThinkPHP的数据库交互必须使用其特定的封装方法,或者可以认为其是对PHP数据库操作的进一步封装,以达到更加安全和高效。

ThinkPHP内置了抽象数据库访问层,把不同的数据库操作封装起来,我们只需要使用公共的Db类进行操作,而无需针对不同的数据库写不同的代码和底层实现,Db类会自动调用相应的数据库驱动来处理。采用PDO方式,目前包含了Mysql、SqlServer、PgSQL、Sqlite等数据库的支持。

数据库连接配置
在APP目录下的database.php中进行相关参数的配置

PHP之ThinkPHP框架(数据库)

测试准备工作

建立数据表tb_test,并在database.php中配置('prefix'  => 'tb_',)使表前缀为tb_,然后建立模板Test.php,控制器Testx.php

PHP之ThinkPHP框架(数据库)

控制器Testx.php实现

 1 <?php
 2 //其中user为模块名,需对应更改
 3 namespace appusercontroller;
 4 
 5 //导入建立的模板,其中user为模块名,Test为模板类
 6 use appusermodelTest;
 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 appusermodel;
 4 use thinkModel;
 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 appusermodel;
 4 use thinkModel;
 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 appusermodel;
 4 use thinkModel;
 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 appusermodel;
 4 use thinkModel;
 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 appusermodel;
 4 use thinkModel;
 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条件的补充

PHP之ThinkPHP框架(数据库)

 1 <?php
 2 //其中user为模块名,需对应更改
 3 namespace appusermodel;
 4 use thinkModel;
 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 thinkDb;

 1 <?php
 2 //其中user为模块名,需对应更改
 3 namespace appusermodel;
 4 use thinkDb;
 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/