CI连接多个数据库

参考http://codeigniter.org.cn/user_guide/database/connecting.html手册

首先,在database.php中设置多个数据库的连接信息,这里命名为‘test'

$db['test']['hostname'] = "localhost";

$db['test']['username'] = "root";

$db['test']['password'] = "";

$db['test']['database'] = "database_name";

$db['test']['dbdriver'] = "mysql";

$db['test']['dbprefix'] = "";

$db['test']['pconnect'] = TRUE;

$db['test']['db_debug'] = FALSE;

$db['test']['cache_on'] = FALSE;

$db['test']['cachedir'] = "";

$db['test']['char_set'] = "utf8";

$db['test']['dbcollat'] = "utf8_general_ci";

$db['test']['swap_pre'] = "";

$db['test']['autoinit'] = TRUE;

$db['test']['stricton'] = FALSE;

记得把配置中的pconnect改为false,其他的不变

加载数据库

$this->db1=$this->load->database('default',TRUE);

$this->db2=$this->load->database('test',TRUE);

调用数据库函数

$this->db1->get('news')->result-array();

$this->db1->where('name','test');

$this->db1->get('news')->result-array();

这就可以实现连接多个数据库了

 

你可能感兴趣的:(数据库)