Thinkphp5多數據庫切換

在項目開發中須要Thinkphp5讀取多個數據庫的數據,本文詳細介紹Thinkphp5多數據庫切換php

 

1、在database.php配置默認數據庫鏈接mysql

 




'type'           => 'mysql',
'hostname'       => '服務器IP地址',
'database'       => '數據庫名',
'username'       => '用戶名',
'password'       => '密碼',
'hostport'       => '數據庫端口',

2、在config.php配置第二個數據庫鏈接sql



'DB_Config_1'=>[
'type'           => 'mysql',
'hostname'       => '服務器IP地址',
'database'       => '數據庫名',
'username'       => '用戶名',
'password'       => '密碼',
'hostport'       => '數據庫端口'
],

3、多數據庫使用

//默認數據庫讀取數據
$test = Db::name("test")->select();
//第二個數據庫讀取數據
$test1=Db::connect("DB_Config_1")->name("test")->select();數據庫

 

在項目開發中須要Thinkphp5讀取多個數據庫的數據,本文詳細介紹Thinkphp5多數據庫切換服務器

 

1、在database.php配置默認數據庫鏈接開發

 




'type'           => 'mysql',
'hostname'       => '服務器IP地址',
'database'       => '數據庫名',
'username'       => '用戶名',
'password'       => '密碼',
'hostport'       => '數據庫端口',

2、在config.php配置第二個數據庫鏈接test



'DB_Config_1'=>[
'type'           => 'mysql',
'hostname'       => '服務器IP地址',
'database'       => '數據庫名',
'username'       => '用戶名',
'password'       => '密碼',
'hostport'       => '數據庫端口'
],

3、多數據庫使用

//默認數據庫讀取數據
$test = Db::name("test")->select();
//第二個數據庫讀取數據
$test1=Db::connect("DB_Config_1")->name("test")->select();配置

相關文章
相關標籤/搜索