Welcome Guest, Not a member yet? Register   Sign In
How to handle DB connection errors
#6

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:

Code:
$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:

Code:
    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)
                {
                    $this->display_error('db_unable_to_connect');
                }

                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:

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

to:

Code:
$config['db_debug'] = FALSE;

and check the value that's returned by:

Code:
$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.
Reply


Messages In This Thread
RE: How to handle DB connection errors - by reactionstudio - 05-22-2017, 04:33 AM



Theme © iAndrew 2016 - Forum software by © MyBB