Welcome Guest, Not a member yet? Register   Sign In
Switch database based on parameter

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?
The largest Bengali tutorial site on Web Development in this planet

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/da...ation.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/da...a-database

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.

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

$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.

(This post was last modified: 04-22-2015, 12:56 AM by rejoan.)

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);
$d_result $sth->fetchAll(PDO::FETCH_ASSOC);
// We are done with PDO for this purpose so free up some resources!
$dbh null;

$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => $d_result[0]['db_username'],
'password' => $d_result[0]['db_pass'],
'database' => $d_result[0]['db_name'],
The largest Bengali tutorial site on Web Development in this planet

a example quick code:

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

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'];
[font=arial, sans-serif][font=monospace, monospace] $configdbfly['database']='sysdbadminis'; /*cambiamos de db*/[/font]
[font=monospace, monospace]  $this->load->database($configdbfly);[/font][/font]

[font=arial, sans-serif][font=monospace, monospace]THE MORE DETAILED AND HOW TO HERE:[/font][/font]

Theme © iAndrew 2016 - Forum software by © MyBB