我在Codeigniter有多个数据库连接有问题。在我的database.php我配置了两个数据库。Codeigniter - 多个数据库连接
$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;
在我的模型我用这个当我要连接到其他数据库比通常一个:
function __construct()
{
parent::__construct();
$this->load->database('hazeleger',TRUE);
}
但在所有的时间笨连接到CMS。当我删除
$active_group = 'cms';
$active_record = FALSE;
Codeingiter出现错误。当我试过这个
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不存在。 我只想在每个模型上选择我想连接的数据库。 但是没有工作。有人知道,我如何在模型上使用不同的数据库 。
非常感谢!
它的作品!非常感谢你! – JelleP
没问题(:如果你想要你可以标记我的答案为“正确”。 – MasterCassim