Welcome Guest, Not a member yet? Register   Sign In
How to Switch Databases in CI
#3

[eluser]XMadMax[/eluser]
I have the same source code for all environments, this allows to be more secure to upload code to prod servers.

In index.php:

Code:
switch ($_SERVER['HTTP_HOST'])
{
case 'www.myserver.com':
  define('ENVIRONMENT', 'production');
break;
case 'www.myserver.local':
  define('ENVIRONMENT', 'development');
break;
case 'www.myserver.myintegrationserver.com':
  define('ENVIRONMENT', 'integration');
break;
}

In your config directory, create this folders:
Code:
development
production
integration

In your config/database.php file, add this line at the end:
Code:
include(strtolower(ENVIRONMENT).'/env_database.php');

The file inside development folder MUST to be different name. If you have ENVIRONMENT defined, Codeigniter tries to load it instead of the base one. The method explained allow you to extend config, firts is loaded the base condig, and overloaded by ypur specific one.

Inside env_database.php:
Code:
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = 'root';
$db['default']['database'] = 'mylocaldatabase';
$db['default']['dbdriver'] = 'mysqli';

All $db default values will be loaded from /config/database.php, in your env_database.php file, you only put the values overloaded.


Messages In This Thread
How to Switch Databases in CI - by El Forum - 07-01-2013, 04:32 AM
How to Switch Databases in CI - by El Forum - 07-03-2013, 11:30 AM
How to Switch Databases in CI - by El Forum - 07-04-2013, 12:23 AM



Theme © iAndrew 2016 - Forum software by © MyBB