CodeIgniter: multiple databases - access to the database configuration in the second database

I studied using multiple databases with CodeIgniter. If I know that the databases are ahead of time, then I can set the information in the configuration file, and then call any database group that I need.

In my situation, however, I need to store this database information in another database. This is a kind of base database with general information about the client, including the database and credentials that are stored in the client data. This provider can add customers every time it wants, and share each customerโ€™s data in different databases.

How can I set the database and credentials based on the values โ€‹โ€‹I return from the main database in CodeIgniter, or is there any way to do this?

Can someone point me in the right direction? Thanks in advance for any advice.

+7
source share
4 answers

From the docs ( http://ellislab.com/codeigniter/user-guide/database/connecting.html ):

The first parameter of this function may optionally be used to specify a specific database group from your configuration file , or you can even send connection values โ€‹โ€‹for a database that is not specified in your config .

So, you would do something like this, replacing the values โ€‹โ€‹with the 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"; $this->load->database($config); 

If you need to maintain a connection to the master database and the client 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"); 
+9
source

Make the default database and client for the second database the master
$ active_group = 'default'; $ active_record = TRUE;

  $db['default']['hostname'] = ''; $db['default']['username'] = ''; $db['default']['password'] = ''; $db['default']['dbdriver'] = ''; $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['default']['swap_pre'] = ''; $db['default']['autoinit'] = TRUE; $db['default']['stricton'] = FALSE; $db['secondDatabase']['hostname'] = ''; $db['secondDatabase']['username'] = ''; $db['secondDatabase']['password'] = ''; $db['secondDatabase']['dbdriver'] = ''; $db['secondDatabase']['dbprefix'] = ''; $db['secondDatabase']['pconnect'] = TRUE; $db['secondDatabase']['db_debug'] = TRUE; $db['secondDatabase']['cache_on'] = FALSE; $db['secondDatabase']['cachedir'] = ''; $db['secondDatabase']['char_set'] = 'utf8'; $db['secondDatabase']['dbcollat'] = 'utf8_general_ci'; $db['secondDatabase']['swap_pre'] = ''; $db['secondDatabase']['autoinit'] = TRUE; $db['secondDatabase']['stricton'] = FALSE; 

you can load the second database in the controller or in the model with

 $DB2 = $this->load->database('secondDatabase', TRUE); 
+6
source

/ ** config / database.php ** /

 $active_group = 'default'; $active_record = TRUE; $db['default']['hostname'] = ''; $db['default']['username'] = ''; $db['default']['password'] = ''; $db['default']['dbdriver'] = ''; $db['default']['dbprefix'] = ''; $db['default']['pconnect'] = TRUE; $db['default']['db_debug'] = (ENVIRONMENT !== 'production'); $db['default']['cache_on'] = FALSE; $db['default']['cachedir'] = ''; $db['default']['char_set'] = 'utf8'; $db['default']['dbcollat'] = 'utf8_general_ci'; $db['default']['swap_pre'] = ''; $db['default']['autoinit'] = TRUE; $db['default']['stricton'] = FALSE; 

/ ** Your controller or model ** /

 //by default the master database will be loaded and you can directly access db using $this->db $result = $this->db->query("SELECT * FROM `your_table`")->limit(1)->get()->result(); $config['dbxyz']['hostname'] = $result->hostname; $config['dbxyz']['username'] = $result->username; $config['dbxyz']['password'] = $result->password; $config['dbxyz']['dbdriver'] = ''; $config['dbxyz']['dbprefix'] = ''; $config['dbxyz']['pconnect'] = TRUE; $config['dbxyz']['db_debug'] = (ENVIRONMENT !== 'production'); $config['dbxyz']['cache_on'] = FALSE; $config['dbxyz']['cachedir'] = ''; $config['dbxyz']['char_set'] = 'utf8'; $config['dbxyz']['dbcollat'] = 'utf8_general_ci'; $config['dbxyz']['swap_pre'] = ''; $config['dbxyz']['autoinit'] = TRUE; $config['dbxyz']['stricton'] = FALSE; //load database config $this->config->load('database'); //Set database config dynamically $this->config->set_item('dbxyz', $config); //Now you can load the new database using $this->dbxyz = $this->load->database('dbxyz'); 

NOTE. For more information, see Code Codeigniter Configuration

+3
source

Add the line below to the \ config \ database.php application

 $db['mydb2']['hostname'] = 'localhost'; $db['mydb2']['username'] = 'root'; $db['mydb2']['password'] = ''; $db['mydb2']['database'] = 'ci2'; $db['mydb2']['dbdriver'] = 'mysql'; $db['mydb2']['dbprefix'] = ''; $db['mydb2']['pconnect'] = TRUE; $db['mydb2']['db_debug'] = TRUE; $db['mydb2']['cache_on'] = FALSE; $db['mydb2']['cachedir'] = ''; $db['mydb2']['char_set'] = 'utf8'; $db['mydb2']['dbcollat'] = 'utf8_general_ci'; $db['mydb2']['swap_pre'] = ''; $db['mydb2']['autoinit'] = TRUE; $db['mydb2']['stricton'] = FALSE; 

Now we use our second database in our controller and model, as shown below.

 $CI = &get_instance(); $this->db2 = $CI->load->database('mydb2', TRUE); $qry = $this->db2->query("SELECT * FROM employee"); print_r($qry->result()); 

I took the link from http://www.tutsway.com/use-multiple-db-connections-in-codeigniter.php . He works for me.

+1
source

All Articles