Codeigniter/PHP check if can connect to database

2019-01-26 15:25发布

问题:

I'd like to backup my read replica(i.e., slave) database with my master database but this simple boolean I did failed:

$config['hostname'] = "myReadReplicaDatabase.com";
//...$config['other_stuff']; other config stuff...
$db_obj=$CI->load->database($config, TRUE);

if(!$db_obj){
     $config['hostname'] = "myMasterDatabase.com";
     $db_obj=$CI->load->database($config, TRUE);
}

After terminating my read replica database I expected the boolean to evaluate to FALSE and the script to then use my master database. Unfortunately, instead I got the following PHP error:

Unable to connect to your database server using the provided settings.
Filename: core/Loader.php

All i want is for the connection to return true or false, does anyone know how to do this in Codeigniter?

回答1:

My question was answered on this thread on Codeigniter forums.

The key is to not autoinitialize the database:

$db['xxx']['autoinit'] = FALSE; 

To suppress errors it you can set this

$db['xxx']['db_debug'] = FALSE; 

Then in your code that checks the db state, check TRUE/FALSE of the initialize() function:

$db_obj = $this->database->load('xxx',TRUE);
  $connected = $db_obj->initialize();
  if (!$connected) {
  $db_obj = $this->database->load('yyy',TRUE);
} 

Here is my entire config file for future reference: https://gist.github.com/3749863.



回答2:

try { 

// do database connection

} catch (Exception $e) {
  // DO whatever you want with the $e data, it has a default __toString() so just echo $e if you want errors or default it to connect another db, etc.
  echo $e->getMessage();

// Connect to secondary DB.
}

For those who downvoted me, you can do this. Exception will catch PDOException.

try {

    $pdo = new PDO($dsn, $username, $password);

} catch(PDOException $e) {

    mail('webmaster@example.com', 'Database error message', $e->getMessage());

    // and finally... attempt your second DB connection.

   exit;

}


回答3:

when you connect to database its returns connection object with connection id on successful condition otherwise return false.

you can check it to make sure that database connection is done or not.

$db_obj=$CI->load->database($config, TRUE);
if($db_obj->conn_id) {
    //do something
} else {
    echo 'Unable to connect with database with given db details.';
}

try this and let me know, if you have any other issue.



回答4:

Based on what was said here: Codeigniter switch to secondary database if primary is down

You can check for the conn_id on the $db_obj

if ($db_obj->conn_id === false) {
    $config['db_debug'] = true;
    $config['hostname'] = "myMasterDatabase.com";
    $db_obj=$CI->load->database($config, TRUE);
}

This should work.



回答5:

$readReplica = @$CI->load->database($config, TRUE); // ommit the error
if ($readReplica->call_function('error') !== 0) {
    // Failed to connect
}

Im not sure about the error code (not sure if its int/string) and don't have CI nearby to test this out but this principle should work



回答6:

I test all I found and nothing wokrs, the only way I found was with dbutil checking if database exists, something like this:

$this->load->database();
$this->load->dbutil();

// check connection details
if( !$this->dbutil->database_exists('myDatabase'))
    echo 'Not connected to a database, or database not exists';