php - CodeIgniter Check if database is wrong -


i'm working on small application connects database , retrieves data tables. need display caller wasn't able connect specified host. how can accomplish that? i've tried use snippet 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 logs listed:

error - 2015-04-09 15:18:19 --> severity: warning  --> mysqli_connect(): (hy000/2005): unknown mysql server host 'localhosta' (2) 

but need check in runtime (after trying connect, obviously) if database config params wrong (host/user/password) , echo error msg caller. $this->db->_error_number() , $this->db->_error_message() not giving me error msgs. ideas? hope clear enough. thanks.

edit:

here's 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 merged real db config array in 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 error in logs 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.

removing last parameter of $this->load->database (which defaults false) did rest of job.

this new code controller:

$this->connected = false;  $this->load->database(array_merge(array(     'hostname' => "localhost",     'username' => "root",     'password' => "root",     'database' => "automaserv",     'dbdriver' => "mysqli",     'dbprefix' => "",     'pconnect' => false,     'db_debug' => false,     'cache_on' => false,     'cachedir' => "",     'char_set' => "utf8",     'dbcollat' => "utf8_general_ci", ), (array) $json->conn));  if ($this->db->initialize() === false) {     die('not connected!'); }  $this->connected = true; 

thanks.


Popular posts from this blog