To connect more than one database simultaneously, do the following,
$db1 = $this->load->database(‘group_one’, TRUE);
$db1 = $this->load->database(‘group_two’, TRUE);
To connect multiple databases in CodeIgniter, you can follow these steps:
- Configure Database Connections: In the
application/config/database.php
file, configure multiple database connections by adding arrays for each database you want to connect to. You’ll typically find a section like$db['default']
for the default database configuration. You can add additional arrays such as$db['second_db']
,$db['third_db']
, etc., each with its own connection settings. - Load Database in Controllers or Models: In your controller or model where you need to interact with a specific database, load the database configuration by specifying the database group you want to use. You can do this using the
$this->load->database()
method in controllers or models.php$this->load->database('second_db'); // Load the 'second_db' group
- Perform Database Operations: Once you’ve loaded the desired database configuration, you can perform database operations as usual using CodeIgniter’s active record or Query Builder methods.
Here’s a brief example:
php
// Load the 'second_db' group
$this->load->database('second_db');
// Perform a query
$query = $this->db->query('SELECT * FROM your_table');
$result = $query->result();
This will execute the query on the database configured under the ‘second_db’ group.
Remember to properly handle errors and close connections when you’re done using them.