Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter: Multiple Databases - Accessing database config in a second database

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.

like image 418
spacemunkee Avatar asked Mar 11 '13 20:03

spacemunkee


3 Answers

From the docs ( https://www.codeigniter.com/user_guide/database/connecting.html ) :

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";

$this->load->database($config);

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");
like image 129
swatkins Avatar answered Oct 18 '22 22:10

swatkins


Make the master a default database and the customer for second database
$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 controller or in model by

$DB2 = $this->load->database('secondDatabase', TRUE); 
like image 45
jalborres Avatar answered Oct 19 '22 00:10

jalborres


/** 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 details, refer Config Class Codeigniter documentation

like image 28
Girish Avatar answered Oct 19 '22 00:10

Girish