CodeIgniter检查数据库是否错误


CodeIgniter Check if database is wrong

我正在开发一个连接到数据库并从表中检索数据的小型应用程序。但我需要向调用者显示我无法连接到指定的主机。我怎样才能做到这一点?我试过使用这个片段,但它不起作用:

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

在我的日志中,我列出了以下内容:

ERROR - 2015-04-09 15:18:19 --> Severity: Warning  --> mysqli_connect(): (HY000/2005): Unknown MySQL server host 'localhosta' (2)

但我需要在运行时(显然是在尝试连接之后)检查数据库配置参数(host/user/password)是否错误,并将错误消息回显给调用者$this->db->error_number()和$this->db->error_message()没有给我错误消息。有什么想法吗?希望我足够清楚。谢谢

编辑:

这是我的数据库配置:

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

但它被合并到我的控制器中的真实数据库配置数组:

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

但我在这条的日志中遇到了一个错误

if ($this->db->initialize() !== FALSE) {

表示

ERROR - 2015-04-09 15:47:48 --> Severity: Notice  --> Undefined property: Main::$db /.../.../...

我不知道出了什么问题。

删除$this->load->database的最后一个参数(默认为FALSE)确实完成了剩下的工作。

这是我的控制器的新代码:

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

谢谢。

您可以将初始连接放入try-catch块

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";
}