Codeigniter - 多个数据库连接 [英] Codeigniter - multiple database connections
问题描述
我在Codeigniter的多个数据库连接有问题。在我的database.php我配置了两个数据库。
I have a problem with multiple db connections at Codeigniter. At my database.php i configured two databases.
$active_group = 'cms';
$active_record = FALSE;
$db['cms']['hostname'] = 'localhost';
$db['cms']['username'] = 'yoloo_cms';
$db['cms']['password'] = 'password';
$db['cms']['database'] = 'yoloo_cms';
$db['cms']['dbdriver'] = 'mysql';
$db['cms']['dbprefix'] = '';
$db['cms']['pconnect'] = TRUE;
$db['cms']['db_debug'] = TRUE;
$db['cms']['cache_on'] = FALSE;
$db['cms']['cachedir'] = '';
$db['cms']['char_set'] = 'utf8';
$db['cms']['dbcollat'] = 'utf8_general_ci';
$db['cms']['swap_pre'] = '';
$db['cms']['autoinit'] = TRUE;
$db['cms']['stricton'] = FALSE;
$db['hazeleger']['hostname'] = 'localhost';
$db['hazeleger']['username'] = 'yoloo_websites';
$db['hazeleger']['password'] = 'password2';
$db['hazeleger']['database'] = 'yoloo_hazeleger';
$db['hazeleger']['dbdriver'] = 'mysql';
$db['hazeleger']['dbprefix'] = '';
$db['hazeleger']['pconnect'] = TRUE;
$db['hazeleger']['db_debug'] = TRUE;
$db['hazeleger']['cache_on'] = FALSE;
$db['hazeleger']['cachedir'] = '';
$db['hazeleger']['char_set'] = 'utf8';
$db['hazeleger']['dbcollat'] = 'utf8_general_ci';
$db['hazeleger']['swap_pre'] = '';
$db['hazeleger']['autoinit'] = TRUE;
$db['hazeleger']['stricton'] = FALSE;
在我的模型我使用这个,当我想连接到另一个数据库比通常的: / p>
At my model i use this when i want to connect to a other db than the usual one:
function __construct()
{
parent::__construct();
$this->load->database('hazeleger',TRUE);
}
但是在任何时候codeigniter都连接到cms。当我删除
But at all time codeigniter connects to cms. When i remove
$active_group = 'cms';
$active_record = FALSE;
Codeingiter出错。当我尝试这个
Codeingiter gives an error. When i tried this
function __construct()
{
parent::__construct();
$db2 = $this->load->database('hazeleger',TRUE);
}
function test()
{
$query = "SELECT * FROM cms_modules";
$result = $db2->db->query($query);
return $db2->result();
}
Variabele db2不存在。
我只想选择,在每个模型,我想连接到db。
但是不工作,t工作。有人知道,我如何可以使用不同的数据库
在模型。
It gives an error. Variabele db2 does not exist. I just want to choose, at every model, wich db i want to connect to. But is doesn,t work. Does somebody know, how i can work with different databases at models.
非常感谢!
推荐答案
变量$ db2作为类字段。你可以访问$ this-> db2 ...
You have to save the variable $db2 as a class field. The you can access $this->db2 ...
这篇关于Codeigniter - 多个数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!