How to add two database connections(localhost,web server) in Codeigniter Database Config?

Viewed 34

I just want to know how to make two active connections in CG(Database Config) one is Localhost the other one is web. I want to have two database connections for fail safe purpose.I have not try to code it yet but an Idea or a sample code will be a great help. (how to add webserver connection?)

here's the single connection(local):

                  $active_group = 'default';
                  $query_builder = TRUE;
                  $db['default'] = array('dsn'  => '',
                  'hostname' => 'localhost',
                  'username' => 'root',
                  'password' => '',
                  'database' => 'projectbox_db',
                  'dbdriver' => 'mysqli',
                  'dbprefix' => '',
                  'pconnect' => FALSE,
                  'db_debug' => TRUE,
                  'cache_on' => FALSE,
                  'cachedir' => '',
                  'char_set' => 'utf8',
                  'dbcollat' => 'utf8_general_ci',
                  'swap_pre' => '',
                  'encrypt' => FALSE,
                  'compress' => FALSE,
                  'stricton' => FALSE,
                  'failover' => array(),
                  'save_queries' => TRUE);
1 Answers
if(ENVIRONMENT == 'production') {
 $active_group = 'default';
                  $query_builder = TRUE;
                  $db['default'] = array('dsn'  => '',
                  'hostname' => 'localhost',
                  'username' => 'server_username',
                  'password' => 'server_password',
                  'database' => 'projectbox_db',
                  'dbdriver' => 'mysqli',
                  'dbprefix' => '',
                  'pconnect' => FALSE,
                  'db_debug' => TRUE,
                  'cache_on' => FALSE,
                  'cachedir' => '',
                  'char_set' => 'utf8',
                  'dbcollat' => 'utf8_general_ci',
                  'swap_pre' => '',
                  'encrypt' => FALSE,
                  'compress' => FALSE,
                  'stricton' => FALSE,
                  'failover' => array(),
                  'save_queries' => TRUE);
} else {
 $active_group = 'default';
                  $query_builder = TRUE;
                  $db['default'] = array('dsn'  => '',
                  'hostname' => 'localhost',
                  'username' => 'root',
                  'password' => '',
                  'database' => 'projectbox_db',
                  'dbdriver' => 'mysqli',
                  'dbprefix' => '',
                  'pconnect' => FALSE,
                  'db_debug' => TRUE,
                  'cache_on' => FALSE,
                  'cachedir' => '',
                  'char_set' => 'utf8',
                  'dbcollat' => 'utf8_general_ci',
                  'swap_pre' => '',
                  'encrypt' => FALSE,
                  'compress' => FALSE,
                  'stricton' => FALSE,
                  'failover' => array(),
                  'save_queries' => TRUE);  
}

You have to change the environment, according to the local code and your production code

Related