2013-02-28 112 views
0

我有一個應用程序需要連接到2個不同的數據庫,並在不同的服務器。有沒有其他的設置,我不得不在做database.php?我寫這在我的代碼連接到這兩個數據庫:CI多個數據庫連接不同的服務器

$provinsi_db = $this->load->database('provinsi', true); //this is from another server 
$local = $this->load->database('default', true); //this one is in my localhost 

,但是當我嘗試選擇從服務器數據庫數據來看,什麼都沒有發生..是沒有問題的選擇從本地數據庫數據,但..任何人都可以幫我?

這是我爲database.php:

$active_group = 'default'; 
$active_record = TRUE; 

$db['default']['hostname'] = 'localhost'; 
$db['default']['username'] = 'username_local'; 
$db['default']['password'] = 'password_local'; 
$db['default']['database'] = 'db_local'; 
$db['default']['dbdriver'] = 'mysql'; 
$db['default']['dbprefix'] = ''; 
$db['default']['pconnect'] = TRUE; 
$db['default']['db_debug'] = TRUE; 
$db['default']['cache_on'] = FALSE; 
$db['default']['cachedir'] = ''; 
$db['default']['char_set'] = 'utf8'; 
$db['default']['dbcollat'] = 'utf8_general_ci'; 
$db['default']['swap_pre'] = ''; 
$db['default']['autoinit'] = TRUE; 
$db['default']['stricton'] = FALSE; 

$db['provinsi']['hostname'] = 'xxx.xxx.xxx.xxx'; 
$db['provinsi']['username'] = 'username_foreign'; 
$db['provinsi']['password'] = 'password_foreign'; 
$db['provinsi']['database'] = 'db_foreign'; 
$db['provinsi']['dbdriver'] = 'mysql'; 
$db['provinsi']['dbprefix'] = ''; 
$db['provinsi']['pconnect'] = TRUE; 
$db['provinsi']['db_debug'] = TRUE; 
$db['provinsi']['cache_on'] = FALSE; 
$db['provinsi']['cachedir'] = ''; 
$db['provinsi']['char_set'] = 'utf8'; 
$db['provinsi']['dbcollat'] = 'utf8_general_ci'; 
$db['provinsi']['swap_pre'] = ''; 
$db['provinsi']['autoinit'] = TRUE; 
$db['provinsi']['stricton'] = FALSE; 

我一定要包括在服務器的「主機名」端口?

+0

你設置爲'database.php' DB既CONFIGS?你可以告訴我們你的'database.php'嗎? – deadlock 2013-02-28 08:26:20

+0

是的,我有,我現在只包括它..我不認爲它有任何問題。 – user2118738 2013-03-01 03:30:59

回答

1

我的整個數據庫設置樣品

//我database.php中

/* API Database Connection */ 

$active_group = 'apidb'; 
$active_record = TRUE; 

$db['apidb']['hostname'] = 'localhost'; 
$db['apidb']['username'] = 'user_name'; 
$db['apidb']['password'] = 'pass_word'; 
$db['apidb']['database'] = 'db_name'; 
$db['apidb']['dbdriver'] = 'mysql'; 
$db['apidb']['dbprefix'] = ''; 
$db['apidb']['pconnect'] = FALSE; 
$db['apidb']['db_debug'] = TRUE; 
$db['apidb']['cache_on'] = FALSE; 
$db['apidb']['cachedir'] = ''; 
$db['apidb']['char_set'] = 'utf8'; 
$db['apidb']['dbcollat'] = 'utf8_general_ci'; 
$db['apidb']['swap_pre'] = ''; 
$db['apidb']['autoinit'] = TRUE; 
$db['apidb']['stricton'] = FALSE; 


/* Site Database Connection */ 

$active_group = 'sitedb'; 
$active_record = TRUE; 

$db['sitedb']['hostname'] = 'localhost'; 
$db['sitedb']['username'] = 'user_name'; 
$db['sitedb']['password'] = 'pass_word'; 
$db['sitedb']['database'] = 'db_name'; 
$db['sitedb']['dbdriver'] = 'mysql'; 
$db['sitedb']['dbprefix'] = ''; 
$db['sitedb']['pconnect'] = FALSE; 
$db['sitedb']['db_debug'] = TRUE; 
$db['sitedb']['cache_on'] = FALSE; 
$db['sitedb']['cachedir'] = ''; 
$db['sitedb']['char_set'] = 'utf8'; 
$db['sitedb']['dbcollat'] = 'utf8_general_ci'; 
$db['sitedb']['swap_pre'] = ''; 
$db['sitedb']['autoinit'] = TRUE; 
$db['sitedb']['stricton'] = FALSE; 

在控制器,裝載數據庫

$this->sitedb = $this->load->database('sitedb', TRUE); 
$this->apidb = $this->load->database('apidb', TRUE); 

在模型中,你可以叫

$this->apidb->query('your query'); 

$this->sitedb->query('your query'); 
+0

它的工作原理!謝謝。我實際上是在PHP和CI新,所以非常感謝:) – user2118738 2013-03-01 10:37:54

+0

高興地幫助:) – Dino 2013-03-01 10:41:09

+0

我也有兩個數據庫的示例問題,如果每個數據庫相同的服務器主機它工作正常,但每一個差異它不工作 – Meas 2015-03-05 05:03:15

0

嘗試在你的配置database.php中改變這個

$db['provinsi']['pconnect'] = FALSE; 

$db['default']['pconnect'] = FALSE; 
+0

我已經試過了,但仍然無法正常工作..我不知道什麼是錯的 – user2118738 2013-03-01 03:29:38

相關問題