Syed Rizwan Ali Syed Rizwan Ali - 7 months ago 22
SQL Question

Connecting two databases in Codeigniter

I am conneting two databases in codeigniter. My database.php configuration is as follows.

$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'dvrs',
'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
);

$db['orcl_db'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'mvrs',
'password' => 'mvrs',
'database' => 'MVRS',
'dbdriver' => 'oci8',
'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
);


Now I am autoloading the default database, and loading the orcl_db in the respective model on demand using

$this->orclDB = $this->database->load("orcl_db", TRUE);


I am connecting to both dbs and running queries successfully.

I need to make sure that the oracle server is available before connecting to it and display proper error messages if the server is not available / not responding.

What will be the best way to do this?

Answer

Following is the code to ensure it.

$this->orclDB = $this->load->database('orcl_db', TRUE);

        if (!$this->orclDB ->initialize()) {
            $response["status"] = false;
            $response["message"] = "Oracle DB is not available.";
        }
Comments