thinkphp? ?? ?? ?? ?? PHP ????? ? ?????. ? ??????? ??? ??? ? ?? ?? ??? ??? ?? ? ??? ?? ??? ??? ?????. ? ? ??? ??????? ???? ? ? thinkphp? ???? ? ?????? ?? ??? ???? ??? ??????
? ???? thinkphp ??????? ? ??????? ???? ???? ??? ?????.
- ?????? ??
thinkphp ????? ?? config
???? database.php
??? ??? ?? ?????. ? ????? ??? ?? ?? ?????? ?? ??? ? ? ????. config
文件夾內(nèi),找到 database.php
文件,打開并進行修改。在該文件中,我們可以看到如下默認的數(shù)據(jù)庫配置信息:
return?[ ????//?默認數(shù)據(jù)庫配置 ????'type'?????????=>?'mysql', ????'hostname'?????=>?'127.0.0.1', ????'database'?????=>?'', ????'username'?????=>?'root', ????'password'?????=>?'', ????...
我們需要將其復(fù)制并新建一個配置項,用于第二個數(shù)據(jù)庫的鏈接。例如:
return?[ ????//?默認數(shù)據(jù)庫配置 ????'type'?????????=>?'mysql', ????'hostname'?????=>?'127.0.0.1', ????'database'?????=>?'', ????'username'?????=>?'root', ????'password'?????=>?'', ????//?第二個數(shù)據(jù)庫配置 ????'db2'?=>?[ ????????'type'?????????=>?'mysql', ????????'hostname'?????=>?'127.0.0.1', ????????'database'?????=>?'', ????????'username'?????=>?'root', ????????'password'?????=>?'', ????], ????...
在該配置項中,我們將第二個數(shù)據(jù)庫的配置信息寫在一個名為 db2
的數(shù)組中,并設(shè)置用戶名、密碼、主機名等信息。
- 連接數(shù)據(jù)庫
在使用第二個數(shù)據(jù)庫前,需要先對其進行連接。我們可以在控制器中的 initialize()
方法中進行數(shù)據(jù)庫的連接。
use?think\Db; class?Index { ????public?function?initialize() ????{ ????????Db::connect('db2')->connect();??//?連接第二個數(shù)據(jù)庫 ????} ????public?function?index() ????{ ????????//?進行查詢操作 ????} }
在上述代碼中,Db::connect()
方法用于指定連接哪個數(shù)據(jù)庫,“db2” 對應(yīng)上一步中添加的配置項名稱。使用 connect()
方法進行連接。
- 查詢操作
進行連接后,便可以在代碼中使用 db()
方法來指定使用哪個數(shù)據(jù)庫進行查詢操作。例如:
use?think\Db; class?Index { ????public?function?initialize() ????{ ????????Db::connect('db2')->connect();??//?連接第二個數(shù)據(jù)庫 ????} ????public?function?index() ????{ ????????$result?=?Db::name('table2',?'db2')->select();??//?查詢第二個數(shù)據(jù)庫中的表格數(shù)據(jù) ????} }
在上面的代碼中,我們對第二個數(shù)據(jù)庫的名為 table2
的表格進行了查詢操作,并將結(jié)果存儲在 $result
中。name()
rrreee
rrreee
? ?? ????? ? ?? ??????? ?? ???db2
?? ??? ?? ??? ??, ????, ??? ?? ? ?? ??? ?????.
- ??????? ????????? ?? ??????? ???? ?? ?? ??????? ???? ???. ?????
initialize()
???? ?? ??????? ??? ? ????. ??rrreee??? ???? Db::connect()
???? ??? ??????? ???? ? ????, "db2"? ?? ???? ??? ?? ??? ??? ?????. . ????? connect()
???? ?????. ??- ???? ?????????? ? ???
db()
???? ???? ?? ??? ??? ??????? ??? ? ????. ?: ??rrreee??? ????? ? ?? ??????? table2
?? ???? ?? ?? ??? ???? ??? $result
? ?????. name()
???? ??? ???? ??? ???? ? ?????. "db2" ????? ??? ? ?? ???????? ???? ?????. ????? ??? ?? ??? ???? ?? ?? thinkphp?? ???? ?? ??? ?? ?? ??? ??? ?? ????. ???????? thinkphp ??????? ? ??????? ???? ?? ??? ???? ??? ?????. ??? ??????? ?? ??? ????? ??? ???? ??? ?? ? ???? ??? ????? ?? ????. ??? ??? thinkphp?? ? ?????? ?? ??? ???? ??? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)