2012-04-15 179 views
0

我可以使用多個數據庫 主要連接的連接是從database.php中多個數據庫連接

但所述另一個在從模型 ,我想

這樣

它之間切換
 $config['remote']['hostname'] = 'localhost'; 
     $config['remote']['username'] = 'root'; 
     $config['remote']['password'] = ''; 
     $config['remote']['database'] = 'countries'; 
     $config['remote']['dbdriver'] = 'mysql'; 
     $config['remote']['dbprefix'] = ''; 
     $config['remote']['pconnect'] = TRUE; 
     $config['remote']['db_debug'] = TRUE; 
     $config['remote']['cache_on'] = FALSE; 
     $config['remote']['cachedir'] = ''; 
     $config['remote']['char_set'] = 'utf8'; 
     $config['remote']['dbcollat'] = 'utf8_general_ci'; 
     $config['remote']['swap_pre'] = ''; 
     $config['remote']['autoinit'] = TRUE; 
     $config['remote']['stricton'] = FALSE; 

     $this->load->database($config); 

     $this->load->database('remote', TRUE); 
+0

此代碼必須在模型 – 2012-04-15 06:03:12

回答

1

使用多個數據庫的基本語法如下。

$DB1 = $this->load->database('group_one', TRUE); 
$DB2 = $this->load->database('group_two', TRUE); 

唯一區別這裏是您必須使用返回的數據庫對象。

查看code igniter user guide筆記上連接到多個數據庫部分

注:更改單詞「group_one」和「group_two」到要連接到特定的組名(或者你可以通過連接值如上所示)。

通過將第二個參數設置爲TRUE(布爾值),該函數將返回數據庫對象。

+0

我不會到config從**模型第二數據庫** – 2012-04-15 06:14:03

+0

或如果我能向參數傳遞到在模型 – 2012-04-15 06:26:03

+0

連接我上述方法適用於任何班級。它可以是控制器或模型。請參閱http://codeigniter.com/user_guide/general/models.html#conn – 2012-04-15 07:04:49