CodeIgniter MultiDatabse connect error

社会主义新天地 提交于 2019-12-10 15:44:34

问题


I see a lot of method/tutorial to connect multi database in CodeIgniter? Still far far away with me. Some one provide me to connect multi database in CI. This is my using way i was found it in a blog. pardon me, i don't recognize the blog address.

    $active_group = 'default';
    $active_record = TRUE;
    $db['default']['hostname'] = 'localhost';
    $db['default']['username'] = 'root';
    $db['default']['password'] = 'root';
    $db['default']['database'] = 'vk_global';
    $db['default']['dbdriver'] = 'mysql';
    $db['default']['dbprefix'] = '';
    $db['default']['pconnect'] = TRUE;
    $db['default']['db_debug'] = TRUE;
    $db['default']['cache_on'] = FALSE;
    $db['default']['cachedir'] = '';
    $db['default']['char_set'] = 'utf8';
    $db['default']['dbcollat'] = 'utf8_general_ci';
    $db['default']['swap_pre'] = '';
    $db['default']['autoinit'] = TRUE;
    $db['default']['stricton'] = FALSE;

    //add alternate database settings
    $db['wow']['hostname'] = 'localhost';
    $db['wow']['username'] = 'root';
    $db['wow']['password'] = 'root';
    $db['wow']['database'] = 'auth';
    $db['wow']['dbdriver'] = 'mysql';
    $db['wow']['dbprefix'] = '';
    $db['wow']['pconnect'] = TRUE;
    $db['wow']['db_debug'] = TRUE;
    $db['wow']['cache_on'] = FALSE;
    $db['wow']['cachedir'] = '';
    $db['wow']['char_set'] = 'utf8';
    $db['wow']['dbcollat'] = 'utf8_general_ci';
    $db['wow']['swap_pre'] = '';
    $db['wow']['autoinit'] = TRUE;
    $db['wow']['stricton'] = FALSE;

i was call it with this code

    $DB2 = $this->load->database('auth',true);

and i was insert a simple row with this code

    $this->$DB2->insert('events_grouptable',$data);

i got this error You have specified an invalid database connection group. how should i solve this error?


回答1:


$DB1 = $this->load->database('default', TRUE);
$DB2 = $this->load->database('wow', TRUE);

You need to set which db to use with which group otherwise the alternative does not work as

$active_group = 'default';

More details are here http://ellislab.com/codeigniter/user-guide/database/connecting.html



来源:https://stackoverflow.com/questions/9543855/codeigniter-multidatabse-connect-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!