CodeIgniter Forums

Full Version: switch the default database on the fly
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Dave Rau[/eluser]
Often I have the need to change databases but from within the same CI app; what methods do you folks use to swap databases (and/or tables) on the fly?

Is there a shortcut I can use to specify multiple database names, but keeping the same connection settings like server name, user/pass, etc.?

El Forum

[eluser]GSV Sleeper Service[/eluser]
you'll have to set up each db in the database config using 'groups' - http://ellislab.com/codeigniter/user-gui...ation.html

El Forum

[eluser]Dave Rau[/eluser]
What if all of the config settings are the same except the database name? Is there some shorthand I can use for that?

Code:
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "database_name";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";

$db['other_db']['database'] = "other_database_name";

I want the other_db group to inherit all of the properties of the default group except database name; any suggestions on how to do that?

El Forum

[eluser]johnwbaxter[/eluser]
ctrl C, ctrl V Wink