CodeIgniter Forums
Switch database based on parameter - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Switch database based on parameter (/showthread.php?tid=61504)



Switch database based on parameter - rejoan - 04-21-2015

Hi,
I need to change database.php configuration based on any parameter in controller. Every time parameter change will change the database name,pass etc. Is is possible?


RE: Switch database based on parameter - mwhitney - 04-21-2015

You can configure multiple database groups in your /application/config/database.php (or /application/config/{ENVIRONMENT}/database.php) file: http://www.codeigniter.com/user_guide/database/configuration.html

Then you can load a specific database group by manually connecting to the database and passing in the group name: http://www.codeigniter.com/user_guide/database/connecting.html#manually-connecting-to-a-database


RE: Switch database based on parameter - CroNiX - 04-21-2015

You can also just pass a config array to $this->load->database($config), which sounds like it might work a little better for what you're trying to do by changing parameters on the fly. You might need to close the db connection and then reload it in between changing parameters.


RE: Switch database based on parameter - kilishan - 04-21-2015

Also - you can pass a database configuration array when you load the model. Simply pass it as the third parameter:

Code:
$config['hostname'] = 'localhost';
$config['username'] = 'myusername';
$config['password'] = 'mypassword';
$config['database'] = 'mydatabase';
$config['dbdriver'] = 'mysqli';
$config['dbprefix'] = '';
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;

$this->load->model('model_name', '', $config);

So you could have your controller pull the parameter it needs, and then set the config array as a class var, then use it when loading the models from that class.


RE: Switch database based on parameter - rejoan - 04-22-2015

not helped. Solution is change database.php as follow
PHP Code:
if (isset($_GET['cfg'])) {
    
$efg = (int) $_GET['cfg'];
} else {
    
$efg 1;
}

$dbh = new PDO('mysql:host=localhost;dbname=chat''root''');

$sql 'SELECT db_user,db_name,db_pass FROM clients WHERE id=?';

$sth $dbh->prepare($sql);
$sth->execute(array($efg));
$d_result $sth->fetchAll(PDO::FETCH_ASSOC);
//var_dump($d_result);
// We are done with PDO for this purpose so free up some resources!
$dbh null;
unset(
$dbh);

$db['default'] = array(
    
'dsn' => '',
    
'hostname' => 'localhost',
    
'username' => $d_result[0]['db_username'],
    
'password' => $d_result[0]['db_pass'],
    
'database' => $d_result[0]['db_name'],
.... 



RE: Switch database based on parameter - mckaygerhard - 02-11-2017

a example quick code:

1) go to application/config.php and made a common config:
$config['sysdb'] =  array(
'hostname'=>'localhost','username'=>'dbuser','password'=>'dbuserclave',
'database'=>'', /* this will be changed "on the fly" in controler */
'dbdriver'=>'mysqli','dbprefix'=>'',
'pconnect'=>FALSE,'db_debug'=>TRUE,'cache_on'=>FALSE
);

2) now in u'r controller made "each time u want to change" a database name with same connections parameters:
 $configdbfly = $this->config->config['sysdb'];
 $configdbfly['database']='sysdbadminis'; /*cambiamos de db*/
 $this->load->database($configdbfly);


THE MORE DETAILED AND HOW TO HERE:
http://qgqlochekone.blogspot.com/2017/02/scheme-vs-real-db-how-to-change-on-fly.html