2

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.

3
  • "Unknown MySQL server host 'localhosta'" is quite a clear message for invalid db connection configuration Commented Apr 9, 2015 at 18:32
  • set $db['default']['db_debug'] = FALSE; to show error Commented Apr 9, 2015 at 18:34
  • @Cristik, yea, but I want to get that message inside my controller so I can display it to the user. Dunno how or when that's been written to the logs. '-' Commented Apr 9, 2015 at 18:37

2 Answers 2

1

Removing the last parameter of $this->load->database (which defaults to FALSE) really did the rest of the job.

This is the new code from my 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.

Sign up to request clarification or add additional context in comments.

Comments

0

you can put you initial connection in to a try catch block

try{
   $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);

} catch (exception $e){
    echo "can't connect";

}

2 Comments

nothing as in you still see the error or you don't see the "can't connect"?
By 'nothing happens' I meant that the issue persists. But now it's fixed, as I mentioned in my answer. Thanks, anyways.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.