Reputation: 11649
I want to change some database settings for one part of my program.
In my setup the databse class is autoloaded with a config which looks like this
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '****';
$db['default']['database'] = 'database';
$db['default']['dbdriver'] = 'mysql';
$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;
At one part of the script I want to change the value of $db['default']['keyname']
, how can I do this?
Upvotes: 1
Views: 3742
Reputation: 39
Also you can always use a Global var. Normally I always have 2 databases in place. One for production and one for development. You just check that var to load one or the other.
That concept can be used to load one database in one part of your application and other in other part. However, I would go with the built in solution like Brendan pointed out.
Upvotes: 0
Reputation: 4565
You should be adding another set of credentials instead of changing the existing ones dynamically:
$db['another_db']['hostname'] = 'localhost';
$db['another_db']['username'] = 'root';
$db['another_db']['password'] = '****';
$db['another_db']['database'] = 'database';
$db['another_db']['dbdriver'] = 'mysql';
$db['another_db']['dbprefix'] = '';
$db['another_db']['pconnect'] = TRUE;
$db['another_db']['db_debug'] = TRUE;
$db['another_db']['cache_on'] = FALSE;
$db['another_db']['cachedir'] = '';
$db['another_db']['char_set'] = 'utf8';
$db['another_db']['dbcollat'] = 'utf8_general_ci';
$db['another_db']['swap_pre'] = '';
$db['another_db']['autoinit'] = TRUE;
$db['another_db']['stricton'] = FALSE;
You can load this other database by doing:
$another_db = $this->load->database('another_db', TRUE);
Then use it like normal database driver:
$another_db->select();
...etc
Upvotes: 2
Reputation: 115
Slightly hacky, but you could add something like this to system/database/DB_driver.php (I've used db password change as an example):-
public function update_pw($value) {
$this->password = $value;
}
Then in your project, do
$this->db->update_pw('example');
$this->db->reconnect();
Depending on specifically what you want to change in the config - and also more importantly, why you want to change it - this may or may not be the best option.
Upvotes: 0
Reputation: 4686
There is how class > check it http://codeigniter.com/user_guide/libraries/config.html
How ever you can NOT change db connection settings.... They are loaded long before your configs..
Upvotes: 2