Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php CodeIgniter检查数据库是否错误_Php_Mysql_Codeigniter - Fatal编程技术网

Php CodeIgniter检查数据库是否错误

Php CodeIgniter检查数据库是否错误,php,mysql,codeigniter,Php,Mysql,Codeigniter,我正在开发一个连接到数据库并从表中检索数据的小应用程序。但是我需要向调用者显示我无法连接到指定的主机。我怎样才能做到这一点?我尝试使用此代码段,但它不起作用: define('ERROR_STR', 'ERROR: '); # ... if ($this->db->_error_number() OR $this->db->_error_message()) { die(ERROR_STR . $this->db->_error_number() .

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

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)
但我需要在运行时检查(显然,在尝试连接之后)数据库配置参数是否错误(主机/用户/密码),并将错误消息回显给调用者$这个->数据库->错误号()和$this->数据库->错误号()没有给我那个错误消息。 有什么想法吗?希望我说得够清楚。谢谢

编辑:

这是我的数据库配置:

$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;
但它被合并到我的控制器中的真实db config数组中:

    $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;
$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;
但是我的日志中有一个错误

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

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

我不知道出了什么问题

您可以将初始连接放入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";

}

删除
$this->load->database
(默认为
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;
$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;

谢谢。

“未知的MySQL服务器主机'localhosta'”对于无效的数据库连接配置集$db['default']['db_debug']=FALSE是一条非常明确的消息;显示error@Cristik,是的,但我想在我的控制器中获取该消息,以便向用户显示它。不知道这是如何或何时写入日志的没有什么,如您仍然看到错误或您没有看到“无法连接”?我说的“什么都没有发生”是指问题仍然存在。但现在它是固定的,正如我在回答中提到的。无论如何,谢谢你。