简体   繁体   English

在Codeigniter中连接第二个数据库时出现问题

[英]Issue connecting second database in Codeigniter

I have to use two databases in a controller. 我必须在控制器中使用两个数据库。 Even though I load the second database with second parameter TRUE to get reference without overwriting the default database it is replacing the default database. 即使我使用第二个参数TRUE加载第二个数据库以获取引用而又不覆盖默认数据库,它仍在替换默认数据库。 Any idea what should be done when loading the second database. 任何想法加载第二个数据库时应该做什么。 Currently I am loading like below. 目前,我正在加载如下。

$testDB = $this->load->database('preview',TRUE,FALSE);

set the pconnect param of both db to false 将两个数据库的pconnect参数设置为false

$db['preview']['pconnect'] = FALSE;

in your config/database.php 在您的config / database.php中

then on your model which is connecting to the second database, load it like this: 然后在要连接到第二个数据库的模型上,像这样加载它:

class Example_m extends CI_Model {

   function __construct(){
        $CI =& get_instance();
        parent::__construct();
        $this->db = $CI->load->database('preview',TRUE);
    }

//this will work sure or check your errors //这可以正常工作或检查您的错误

//If you use group

//group name
$preview_DB = $this->load->database('preview',TRUE);

//another DB

$another_DB = $this->load->database('another',TRUE);

//if You use array

//create connection string
$db = array();
$db['hostname'] = "database_hostname";
$db['username'] = "database_username";
$db['password'] = "database_password";
$db['database'] = "database_name";
$db['dbdriver'] = 'mysql';
$db['pconnect'] = FALSE;


$DB = $this->load->database($db,TRUE);

I assume you've made the required configuration in your database.php 我假设您已经在database.php进行了所需的配置

you can load your 2nd database like this: 您可以像这样加载第二个数据库:

$testDB = $this->load->database('preview', TRUE);

and use it like this: 并像这样使用它:

$query = $testDB->query("SELECT * FROM TABLE");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM