It is possible to add different database settings for different environments like development, testing and production.
CodeIgniter has a config file that lets you store your database connection values (username, password, database name, etc.). The config file is located at application/config/database.php. You can also set database connection values for specific environments by placing database.php it the respective environment config folder.
The config settings are stored in a multi-dimensional array with this prototype:
$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['default']['swap_pre'] = "";
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
$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['default']['swap_pre'] = "";
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
The reason we use a multi-dimensional array rather than a more simple one is to permit you to optionally store multiple sets of connection values. If, for example, you run multiple environments (development, production, test, etc.) under a single installation, you can set up a connection group for each, then switch between groups as needed.
Here is the process to do it.
1) Suppose you want to take two different db settings for ‘development’ and ‘production’. So copy the database.php file from /bonfire/application/config/database.php
2) Create a folder called “development” in /bonfire/application/config folder and paste the copied database.php file in step 1 inside this folder, so your path becomes /bonfire/application/config/development/database.php
Now write/change the following lines in the file.
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$active_group = 'development';
$active_record = TRUE;
$db['development']['hostname'] = 'localhost';
$db['development']['username'] = 'root';
$db['development']['password'] = '';
$db['development']['database'] = 'techshu_b2b_printing';
$db['development']['dbdriver'] = 'mysql';
$db['development']['dbprefix'] = 'bf_';
$db['development']['pconnect'] = TRUE;
$db['development']['db_debug'] = TRUE;
$db['development']['cache_on'] = FALSE;
$db['development']['cachedir'] = '';
$db['development']['char_set'] = 'utf8';
$db['development']['dbcollat'] = 'utf8_general_ci';
$db['development']['swap_pre'] = '';
$db['development']['autoinit'] = TRUE;
$db['development']['stricton'] = TRUE;
3) Create a folder called “production” in /bonfire/application/config folder and paste the copied database.php file in step 1 inside this folder, so your path becomes /bonfire/application/config/production/database.php
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$active_group = 'production';
$active_record = TRUE;
$db['production']['hostname'] = 'mysql.techshu.com';
$db['production']['username'] = 'techshuuser';
$db['production']['password'] = 'techshupass12';
$db['production']['database'] = 'techshu_b2b_printing';
$db['production']['dbdriver'] = 'mysql';
$db['production']['dbprefix'] = 'bf_';
$db['production']['pconnect'] = TRUE;
$db['production']['db_debug'] = TRUE;
$db['production']['cache_on'] = FALSE;
$db['production']['cachedir'] = '';
$db['production']['char_set'] = 'utf8';
$db['production']['dbcollat'] = 'utf8_general_ci';
$db['production']['swap_pre'] = '';
$db['production']['autoinit'] = TRUE;
$db['production']['stricton'] = TRUE;
4) Now delete the database settings in /bonfire/application/config/database.php so that your file now looks like this:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* End of file database.php */
/* Location: ./application/config/database.php */
5) Now change the following line in /index.php
define('ENVIRONMENT', 'development');
to this
switch($_SERVER['HTTP_HOST'])
{
case ‘www.example.com’:
define('ENVIRONMENT', 'production');
break;
case ‘testing.example.com’:
default:
define('ENVIRONMENT', 'development');
break;
}
Where www.example.com and testing.example.com are the production and testing sites respectively.