2012-04-15 181 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