Syed Rizwan Ali
Syed Rizwan Ali

Reputation: 233

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?

Upvotes: 1

Views: 673

Answers (2)

Syed Rizwan Ali
Syed Rizwan Ali

Reputation: 233

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.";
        }

Upvotes: 1

Vali S
Vali S

Reputation: 1461

In order to turn off db debugging, which throws fatal error when db connection fails, use $db['orcl_db']['db_debug'] = FALSE; in your database config file. Then you can check if database is loaded like this:

if ( $this->load->database('orcl_db') === FALSE )
{
   // do whatever you think is appropriate, but do not panick
}

Upvotes: 0

Related Questions