CI连接多个数据库
Jun 24 , 2015

参考http://codeigniter.org.cn/user_guide/database/connecting.html手册
首先,在database.php中设置多个数据库的连接信息,这里命名为’test’
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$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,其他的不变
加载数据库
1
2
$this->db1=$this->load->database('default',TRUE);
$this->db2=$this->load->database('test',TRUE);
调用数据库函数
1
2
3
$this->db1->get('news')->result-array();
$this->db1->where('name','test');
$this->db1->get('news')->result-array();
这就可以实现连接多个数据库了

NEXT
 |