Home > Backend Development > PHP Tutorial > CI怎么配置多数据库访问

CI怎么配置多数据库访问

PHPz
Release: 2020-09-05 10:08:26
Original
1257 people have browsed it

CI配置多数据库访问的方法:首先打开“datebase.php”文件;然后修改代码为“$db['XXX']['pconnect'] = FALSE;”;接着将原来的数据库链接定义复制一份;最后完成配置使用即可。

CI怎么配置多数据库访问

CI怎么配置多数据库访问?

本文实例讲述了CI配置多数据库访问的方法。分享给大家供大家参考,具体如下:

1、修改datebase.php文件,将:

$db['XXX']['pconnect'] = TRUE;
Copy after login

修改成:

$db['XXX']['pconnect'] = FALSE;
Copy after login

2、将原来的数据库链接定义复制一份,如当前

$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;
其他的都不需要修改,然后在你的控制器里面加载 构造函数:
function __construct()
{
  parent::__construct();
  $this->XXX= $this->load->database('XXX', TRUE);
  //注意,这里的XXX代表上面的bbs,修改成bbs就行了
}
Copy after login

使用方式:

$query = $this->bbs->query($sql);
//和以前一样,就是这里用到了$this->XXX-> XXX就是你定义的bbs
$rs = $query->result();
Copy after login

更多相关知识,请访问PHP中文网

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template