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

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);

if(
$this->db->conn_id->connect_error) {
 
   //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.
Reply


Messages In This Thread
How to handle DB connection errors - by guilhermemuller - 05-15-2017, 11:42 AM



Theme © iAndrew 2016 - Forum software by © MyBB