码迷,mamicode.com
首页 > 数据库 > 详细

CI连接多个数据库

时间:2015-06-24 16:02:28      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

参考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();

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

 

CI连接多个数据库

标签:

原文地址:http://www.cnblogs.com/liuyfl/p/4597852.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!