I'm working on a small application that connects to a database and retrieves data from tables. But I need to display to the caller that I wasn't able to connect to the specified host. How can I accomplish that? I've tried to use this snippet but it doesnt work:
define('ERROR_STR', 'ERROR: ');
# ...
if ($this->db->_error_number() OR $this->db->_error_message()) {
die(ERROR_STR . $this->db->_error_number() . ': ' . $this->db->_error_message());
}
In my logs I get this listed:
ERROR - 2015-04-09 15:18:19 --> Severity: Warning --> mysqli_connect(): (HY000/2005): Unknown MySQL server host 'localhosta' (2)
But I need to check in runtime (after trying to connect, obviously) if database config params are wrong (host/user/password) and echo that error msg back to the caller. $this->db->_error_number() and $this->db->_error_message() are not giving me that error msgs. Any ideas? Hope I was clear enough. Thanks.
EDIT:
Here's my db config:
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = '';
$db['default']['password'] = '';
$db['default']['database'] = '';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = FALSE;
$db['default']['stricton'] = FALSE;
But it is merged to the real db config array in my controller:
$this->connected = false;
$this->load->database(array_merge(array(
'hostname' => "localhost",
'username' => "username",
'password' => "password",
'database' => "database",
'dbdriver' => "mysqli",
'dbprefix' => "",
'pconnect' => FALSE,
'db_debug' => FALSE,
'cache_on' => FALSE,
'cachedir' => "",
'char_set' => "utf8",
'dbcollat' => "utf8_general_ci",
), (array) $json->conn), TRUE);
if ($this->db->initialize() !== FALSE) {
die('Not CONNECTED!');
}
$this->connected = true;
But I get a error in my logs for this line
if ($this->db->initialize() !== FALSE) {
saying
ERROR - 2015-04-09 15:47:48 --> Severity: Notice --> Undefined property: Main::$db /.../.../...
I have no idea of whats wrong.