Asked  7 Months ago    Answers:  5   Viewed   68 times


$db['default']['hostname'] = "";
$db['default']['username'] = "webuser";
$db['default']['password'] = "----";
$db['default']['database'] = "vad";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";

$db['stats']['hostname'] = "";
$db['stats']['username'] = "webuser";
$db['stats']['password'] = "---";
$db['stats']['database'] = "vad_stats";
$db['stats']['dbdriver'] = "mysql";
$db['stats']['dbprefix'] = "";
$db['stats']['pconnect'] = TRUE;
$db['stats']['db_debug'] = TRUE;
$db['stats']['cache_on'] = FALSE;
$db['stats']['cachedir'] = "";
$db['stats']['char_set'] = "utf8";
$db['stats']['dbcollat'] = "utf8_general_ci";

The issue is I can only define in the configuration one $active_group, default, or stats. I followed the CodeIgniter documentation and I added the following:

$DB2 = $this->load->database('stats', TRUE);

This way I connect to the second database, but I lose the connection to the first one. Does anyone have any ideas on how can I load the two database without having to do the following in all models constructors?

$database1 = $this->load->database('database1', TRUE);
$database2 = $this->load->database('database2', TRUE); 





Instead of applying the hack as mentioned by Camacho you can also set the 'pconnect'-flag in the database.php file to FALSE for all connections.

Wednesday, March 31, 2021
answered 7 Months ago

Problem solved by restarting Apache.

I attach the url which saved me:

Wednesday, March 31, 2021
answered 7 Months ago

From the docs ( ) :

The first parameter of this function can optionally be used to specify a particular database group from your config file, or you can even submit connection values for a database that is not specified in your config file.

So you would do something like this, replacing the values with values from the master database:

$config['hostname'] = "localhost";
$config['username'] = "myusername";
$config['password'] = "mypassword";
$config['database'] = "mydatabase";
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";


If you need to maintain a connection to the master database and the customer database, then change the last line to:

$customer_db = $this->load->database($config, TRUE);

// to use the master database:
$this->db->query("SELECT * FROM my_table");

// to then use the customer database:
$customer_db->query("SELECT * FROM whatever");
Monday, August 2, 2021
Abhishek Gupta
answered 3 Months ago

in your database config file add as many configuration groups as the numbers of your databases:

$db['a']['hostname'] = 'localhost';
$db['a']['username'] = 'user';
$db['a']['password'] = 'pw';
$db['a']['database'] = 'db1';

$db['b']['hostname'] = 'localhost';
$db['b']['username'] = 'user';
$db['b']['password'] = 'pw';
$db['b']['database'] = 'db2';

//set the default db
$active_group = 'a';

then on your model initialize a class variable:

private $db_b;

and, into the contructor, set it as follow

   $this->db_b = $this->load->database('b', TRUE); 

now you are able to use the database b as usual:

$this->db_b->query('YOUR QUERY');

and obviously the default one as follow:

$this->db->query('YOUR QUERY');
Tuesday, August 10, 2021
answered 3 Months ago

I'm not sure if you call this "best" way, but a way, as described in the tutorial, is this,

in the database file, you have the default configuration, a part of which is:

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "user";
$db['default']['password'] = "database";
$db['default']['database'] = "db1";

now you can create another group, say we call it group1 and we want it to have everything the same as the default database settings except for the name, so you can do


then, when you want to use the second database, just go

$DB2 = $this->load->database('group1', TRUE); 

and then, instead of $this->db->foo() , you will do $DB2->foo()

alternatively (as suggested in comments by sbaaaang), you can do $this->db=$DB2; to keep everything the same

and you can extend this to multiple groups like this

 $DB1 = $this->load->database('group1', TRUE); 
 $DB2 = $this->load->database('group2', TRUE); 
 $DBn = $this->load->database('groupn', TRUE); 
Wednesday, August 11, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :