Maposa Takalani
Maposa Takalani

Reputation: 338

how to auto connect single database in codeigniter

Normally, when connecting to multiple databases in CodeIgniter, we specify it in database.php like

$db['default']['host_name']='...'; . . .
$db['group_one']['host_name']='...';

and then autoload them in autoload.php.

I now want to only have the first group, which is the default, to connect on each page. I want to active the second group manually. I know there is a way to config database connection at runtime, but I do not want that, since I would need to set the config values everytime I connect to the second db.

How can I accomplish this?

Upvotes: 0

Views: 1131

Answers (2)

Subhra
Subhra

Reputation: 312

In database.php file, change the ‘pconnect’ value of all record sets to FALSE. This will make the “default” group as default connection. Other connections you will have to make manually.

For example, put $db['default']['pconnect'] = FALSE; instead of $db['default']['pconnect'] = TRUE;

To connect to another database, you need to specify the group name as follows: $db2 = $this->load->database(‘new_conn’, TRUE);

Go through following link for more details: http://subhra.me/connecting-to-multiple-databases-in-codeigniter/

Upvotes: 1

user3573738
user3573738

Reputation:

Override in the right place of the controller adjustment model of the active DB configuration. Write Helper function and change inside db config.

Upvotes: 1

Related Questions