Home > Article > Backend Development > How to configure multi-database access in CI, ci database access_PHP tutorial
The example in this article describes the CI configuration method for multi-database access. Share it with everyone for your reference, the details are as follows:
1. Modify the datebase.php file and change:
$db['XXX']['pconnect'] = TRUE;
was changed to:
$db['XXX']['pconnect'] = FALSE;
2. Copy the original database link definition, such as the current one
$db['bbs']['hostname'] = '你的数据库IP地址'; $db['bbs']['username'] = '链接用户名'; $db['bbs']['password'] = '数据库连接密码'; $db['bbs']['database'] = '数据库名'; $db['bbs']['dbdriver'] = 'mysql'; $db['bbs']['dbprefix'] = 'cg_'; $db['bbs']['pconnect'] = FALSE; $db['bbs']['db_debug'] = TRUE; $db['bbs']['cache_on'] = FALSE; $db['bbs']['cachedir'] = ''; $db['bbs']['char_set'] = 'utf8'; $db['bbs']['dbcollat'] = 'utf8_general_ci'; $db['bbs']['swap_pre'] = ''; $db['bbs']['autoinit'] = TRUE; $db['bbs']['stricton'] = FALSE;
No other modifications are required, and then load the constructor in your controller:
function __construct() { parent::__construct(); $this->XXX= $this->load->database('XXX', TRUE); //注意,这里的XXX代表上面的bbs,修改成bbs就行了 }
How to use:
$query = $this->bbs->query($sql); //和以前一样,就是这里用到了$this->XXX-> XXX就是你定义的bbs $rs = $query->result();
Readers who are interested in more CodeIgniter related content can check out the special topics of this site: "codeigniter introductory tutorial", "CI (CodeIgniter) framework advanced tutorial", "php date and time usage summary", "php object-oriented program" Design introductory tutorial", "php string (string) usage summary", "php mysql database operation introductory tutorial" and "php common database operation skills summary"
I hope this article will be helpful to everyone’s PHP program design based on the CodeIgniter framework.