• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to handle DB connection errors

Hello all,

I have developed a superadmin system that manages an admin interface for a lot of users, each with his own database. It is working well, but there's one situation that I'm not able to handle yet. When logging in, the superadmin supplies the ID of the database he will access. There are three alternatives as for the outcome:
  1. The script retrieves the client's DB info and connects successfully;
  2. The script retrieves the client's DB info and does NOT connect successfully;
  3. The script cannot retrieve any data on that client and DB, and returns an error.
For situations #1 and #3, everything is alright. The problem is when the information for the connection is invalid. I'm using the following code:

PHP Code:
$config['hostname'] = $row->server// from the superadmin DB
$config['username'] = $row->user// from the superadmin DB
$config['password'] = $password// from the superadmin DB
$config['database'] = $row->db// from the superadmin DB
$config['dbdriver'] = 'mysqli';
$config['dbprefix'] = '';
$config['pconnect'] = FALSE;
$config['db_debug'] = (ENVIRONMENT !== 'production');
$config['cache_on'] = FALSE;
$config['cachedir'] = '';
$config['char_set'] = 'utf8';
$config['dbcollat'] = 'utf8_general_ci';

$db_config $config;

$this->db $this->load->database($configTRUE);

$this->db->conn_id->connect_error) {
   return false;

However, in situation #2 the "conn_id" error is not reached. CodeIgniter steps in with the "Unable to connect to your database server using the provided settings." error and that's it.

Is there a way to handle this kind of connection error, so I can redirect to the previous page and inform the user? I don't want the CI error page to show.

Thank you very much for your help.

You can check for an error using:

PHP Code:
$error $this->db->error(); 

You can check the connection using this:

PHP Code:
$connected $this->conn_id

You can always view the CodeIgniter source files to learn a lot of hidden gems also...
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )

Thank you, I'll try that and let you know if it worked.

Unfortunately, it does not work, because when I use $this->load->database($db_config) CodeIgniter already attempts the connection and the error occurs. I can't test for the error before the connection is successful. This is what's bothering me. However, I think that I will have to find another way to do this.

If you need to test it first, you can connect using pure MySQLi code then use CI.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )

I've just had a quick read through the CodeIgniter source code that triggers the error message you're running into.

In the config/database.php file there is the following setting:

$db['default'] = array(
    'db_debug' => TRUE,

and in system/database/DB_driver.php the following code seems to be triggering the error message you're seeing:

    public function initialize()
            // We still don't have a connection?
            if ( ! $this->conn_id)
                log_message('error', 'Unable to connect to the database');

                if ($this->db_debug)

                return FALSE;

After a quick test setting db_debug to FALSE in the database.php config does prevent CodeIgniter from hijacking the request to display the error message, and execution will continue, the initialize method should return FALSE if the connection fails.

Try changing the following code in your application:

$config['db_debug'] = (ENVIRONMENT !== 'production');


$config['db_debug'] = FALSE;

and check the value that's returned by:

$this->load->database($config, TRUE);

in your test case, this may help you to detect a failed connection attempt without the error message hijacking the request. I am testing this on the latest version of CodeIgniter (currently v3.1.4), if you are running an older version this may or may not work for you.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.