本文實(shí)例講述了thinkphp 框架數(shù)據(jù)庫切換實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
數(shù)據(jù)庫配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
//數(shù)據(jù)庫配置1 'db_config1' => [ // 數(shù)據(jù)庫類型 'type' => 'mysql' , // 服務(wù)器地址 'hostname' => '127.0.0.1' , // 數(shù)據(jù)庫名 'database' => 'thinkphp' , // 數(shù)據(jù)庫用戶名 'username' => 'root' , // 數(shù)據(jù)庫密碼 'password' => '' , // 數(shù)據(jù)庫編碼默認(rèn)采用utf8 'charset' => 'utf8' , // 數(shù)據(jù)庫表前綴 'prefix' => 'think_' , ], //數(shù)據(jù)庫配置2 'db_config2' => 'mysql://root:1234@localhost:3306/thinkphp#utf8' ; |
1
2
3
4
|
//默認(rèn)數(shù)據(jù)庫讀取數(shù)據(jù) $test = Db::name( "test" )->select(); //第二個(gè)數(shù)據(jù)庫讀取數(shù)據(jù) $test1 =Db::connect( "DB_Config_1" )->name( "test" )->select(); |
application/config.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
$db1 = [ 'type' => 'mysql' , 'hostname' => '127.0.0.1' , 'database' => 'testA' , 'username' => 'root' , 'password' => '123456' , 'hostport' => '3306' , 'params' =>[], 'charset' => 'utf8' , 'prefix' => '' , ], $db2 = [ 'type' => 'mysql' , 'hostname' => '127.0.0.1' , atabase '=>' testB', 'username' => 'root' , 'password' => '123456' , 'hostport' => '3306' , 'params' =>[], 'charset' => 'utf8' , 'prefix' => '' , ], Db::connect( 'db1' )->query( 'select * from user where age=25' ); |
方法配置
我們可以在調(diào)用Db類的時(shí)候動(dòng)態(tài)定義連接信息,例如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
Db::connect([ // 數(shù)據(jù)庫類型 'type' => 'mysql' , // 數(shù)據(jù)庫連接DSN配置 'dsn' => '' , // 服務(wù)器地址 'hostname' => '127.0.0.1' , // 數(shù)據(jù)庫名 'database' => 'thinkphp' , // 數(shù)據(jù)庫用戶名 'username' => 'root' , // 數(shù)據(jù)庫密碼 'password' => '' , // 數(shù)據(jù)庫連接端口 'hostport' => '' , // 數(shù)據(jù)庫連接參數(shù) 'params' => [], // 數(shù)據(jù)庫編碼默認(rèn)采用utf8 'charset' => 'utf8' , // 數(shù)據(jù)庫表前綴 'prefix' => 'think_' , ]); |
或者使用字符串方式:
1
|
Db::connect( 'mysql://root:1234@127.0.0.1:3306/thinkphp#utf8' ); |
字符串連接的定義格式為:
數(shù)據(jù)庫類型://用戶名:密碼@數(shù)據(jù)庫地址:數(shù)據(jù)庫端口/數(shù)據(jù)庫名#字符集
注意:字符串方式可能無法定義某些參數(shù),例如前綴和連接參數(shù)。
如果我們已經(jīng)在應(yīng)用配置文件(注意這里不是數(shù)據(jù)庫配置文件)中配置了額外的數(shù)據(jù)庫連接信息,例如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
//數(shù)據(jù)庫配置1 'db_config1' => [ // 數(shù)據(jù)庫類型 'type' => 'mysql' , // 服務(wù)器地址 'hostname' => '127.0.0.1' , // 數(shù)據(jù)庫名 'database' => 'thinkphp' , // 數(shù)據(jù)庫用戶名 'username' => 'root' , // 數(shù)據(jù)庫密碼 'password' => '' , // 數(shù)據(jù)庫編碼默認(rèn)采用utf8 'charset' => 'utf8' , // 數(shù)據(jù)庫表前綴 'prefix' => 'think_' , ], //數(shù)據(jù)庫配置2 'db_config2' => 'mysql://root:1234@localhost:3306/thinkphp#utf8' ; |
我們可以改成
1
2
|
Db::connect( 'db_config1' ); Db::connect( 'db_config2' ); |
database.php是框架默認(rèn)的數(shù)據(jù)庫配置,里面寫數(shù)據(jù)庫1的信息,新建了個(gè)database2.php是放置數(shù)據(jù)庫2的信息。
創(chuàng)建完數(shù)據(jù)庫2之后,在config配置文件里,文件最后引入數(shù)據(jù)庫2的配置信息
1
2
|
$db_con2 = require_once ( 'database2.php' ), 'db_con2' => $db_con2 , |
代碼中引用:
選擇數(shù)據(jù)庫1的時(shí)候,我是用模型查詢的直接寫SQL語句:
1
2
3
4
5
|
//模型查詢 $user = new User(); $result = $user ->where( 'username' , $data [ 'username' ]) ->where( 'password' , $data [ 'password' ]) ->find(); |
或者
1
2
3
|
User::where( 'id' , '1' )->find(); //普通結(jié)構(gòu)查詢 Db::table( 'think_user' )->where( 'id' ,1)->find(); |
查詢數(shù)據(jù)庫2的信息時(shí),調(diào)用普通查詢語句:
1
2
3
4
5
|
$list = Db::connect( 'db_con2' ) ->table( 'nrf_amf_reg_info' ) ->alias( 'r' ) ->join( 'nrf_amf_server s' , 'r.Id = s.nrf_amf_reg_Id' , 'LEFT' ) ->paginate(); |
或者
1
|
$list = Db::connect( 'db_con2' )->name( 'nrf_disc_record' )->paginate(); |
注:nrf_amf_reg_info和nrf_disc_record為表名
希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。
原文鏈接:https://blog.csdn.net/qq_42176520/article/details/81007503