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

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

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
Reply
#3

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.
Reply
#4

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.
Reply
#5

(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);
$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'],
.... 
The largest Bengali tutorial site on Web Development in this planet
Reply
#6

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...n-fly.html
Reply




Theme © iAndrew 2016 - Forum software by © MyBB