我在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