0

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. Any idea what should be done when loading the second database. Currently I am loading like below.

$testDB = $this->load->database('preview',TRUE,FALSE);
1
  • Are you doing anything else above or below? Commented Jul 9, 2013 at 9:08

3 Answers 3

1

set the pconnect param of both db to false

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

in your 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);
    }
Sign up to request clarification or add additional context in comments.

Comments

0

//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);

Comments

0

I assume you've made the required configuration in your 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");

1 Comment

Thank you for the answer. But I tried this way also and that also replaces the default database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.