For your case, I believe you have to create an extra setting while running the program. From agreement with the documentation you can change the DB configuration as follows:
<?php
$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);
Or use two settings at the same time:
$dbBase = $this->load->database('default', TRUE);
$dbUser = $this->load->database($config, TRUE);
// Dispara query para o banco principal
$dbBase->query();
// Dispara query para o banco do usuario
$dbUser->query();
Just change in the array $config
the parameters you have in the session (outside the file database.php
).
$config['hostname'] = $this->session->userdata('host');
$config['username'] = $this->session->userdata('user');
$config['password'] = $this->session->userdata('password');
$config['database'] = $this->session->userdata('database');
$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";
I tried this way, but I was not successful, because he always looked for the settings of the file 'database.php'
– Meeeefiu
This you would change not in the configuration file, but in the execution of your controller for example.
– gmsantos
Still unsuccessful, when I do so it returns me an error "Call to a Member Function select() on a non-object in"
– Meeeefiu
@Mathdesigner47 could insert some snippets of code from your application ? I tested here on a new project and it worked.
– gmsantos
Buddy, there was a typo of mine, now it’s working perfectly.
– Meeeefiu