Sadique AMT
Sadique AMT

Reputation: 35

How to setup different database connection based on environment - codeigniter 3

I have set up environment on my codeigniter project as development, production.

I have used this code.

switch($_SERVER["HTTP_HOST"])
{
 case "localhost":
    define('ENVIRONMENT', 'development');
 break;
 default:
    define('ENVIRONMENT', 'production');
 break;
}

Now i have to set different database connection for development and production.

Please help.

Upvotes: 0

Views: 487

Answers (1)

Mudasir Syed
Mudasir Syed

Reputation: 131

Database connections information defined in application/config/database.php file.

you can apply condition on bases of ENVIRONMENT

Open application/config/database.php file.


$db['database2'] = array(
 'hostname' => (ENVIRONMENT !== 'production') ? 'localhost' : 'live_host',
 'username' => (ENVIRONMENT !== 'production') ? 'root' : 'live_user',
 'password' => (ENVIRONMENT !== 'production') ? 'local_password' : 'live_password',
 'database' => (ENVIRONMENT !== 'production') ?  'local_db' : 'live_db',
 'dbdriver' => 'mysqli',
 .......
);

May it helps, Thanks

Upvotes: 2

Related Questions