I've been looking into using multiple databases with CodeIgniter. If I know what the databases are ahead of time, then I can set the information in the config file and then call whichever database group I need.
In my situation, however, I need to store that database information in another database. It is sort of a master database with general information about a customer including the database and credentials that the customer's data is stored in. This vendor can then add customers whenever they want and have each customer's data segregated in different databases.
How can I set the database and credentials based on the values I get back from the master database in CodeIgniter, or is there even a way to do that?
Can anyone point me in the right direction? Thanks in advance for any advice.
From the docs ( https://www.codeigniter.com/user_guide/database/connecting.html ) :
So you would do something like this, replacing the values with values from the master database:
If you need to maintain a connection to the master database and the customer database, then change the last line to:
Make the master a default database and the customer for second database
$active_group = 'default'; $active_record = TRUE;
you can load the second database in controller or in model by
Add below line in application\config\database.php
Now we use our second database in our Controller and model like below.
I have taken reference from http://www.tutsway.com/use-multiple-db-connections-in-codeigniter.php .It's work for me.
/** config/database.php **/
/** Your controller or model **/
NOTE: For more details, refer Config Class Codeigniter documentation