MySQLi,PHP - 尝试{....} catch(...){....}无效

时间:2016-10-28 15:44:07

标签: php mysqli error-handling try-catch php-7

我试图使用mysql捕获php7上的连接错误,但它无法正常工作......

我不明白什么是不正确的:我使用的这段代码的类似部分也在运作......

这一定是我遗忘的基本细节...... 我需要抛出$this->MyErrorFunction($e)

有人可以帮忙吗?

public function connect($credentialset) {
    #$this->driver = new \mysqli_driver();
    #$this->driver->report_mode = MYSQLI_REPORT_ALL;
    mysqli_report(MYSQLI_REPORT_STRICT); 
    try {
        $this->conn->real_connect(
                isset(self::$credentials[$credentialset]['host'])       ? self::$credentials[$credentialset]['host']        : ini_get("mysqli.default_host"),
                isset(self::$credentials[$credentialset]['user'])       ? self::$credentials[$credentialset]['user']        : ini_get("mysqli.default_user"),
                isset(self::$credentials[$credentialset]['password'])   ? self::$credentials[$credentialset]['password']    : ini_get("mysqli.default_pw"),
                isset(self::$credentials[$credentialset]['database'])   ? self::$credentials[$credentialset]['database']    : "",
                isset(self::$credentials[$credentialset]['port'])       ? self::$credentials[$credentialset]['port']        : ini_get("mysqli.default_port"),
                isset(self::$credentials[$credentialset]['socket'])     ? self::$credentials[$credentialset]['socket']      : ini_get("mysqli.default_socket"),
                isset(self::$credentials[$credentialset]['flag'])       ? self::$credentials[$credentialset]['flag']        : null
        );
        $charset = isset(self::$credentials[$credentialset]['charset']) ? self::$credentials[$credentialset]['charset'] : 'utf8mb4';
        $this->conn->set_charset($charset);
    } catch (Exception $e) {
        $this->MyErrorFunction($e);
    }
}
  

致命错误:未捕获mysqli_sql_exception:拒绝访问用户' myinvaliduser' @' 10.241.84.65' (使用密码:是)在第77行的/oxygen-v2/php-bin/datadrivers/mysqlz/mysqlz.php

1 个答案:

答案 0 :(得分:0)

\ O / 我确信错误只是一个愚蠢的事情!

解决了这段代码:

} catch (Exception $e) {
    $this->MyErrorFunction($e);
}

通过此代码:

} catch (\mysqli_sql_exception $e) {
    $this->MyErrorFunction($e);
}

代替Exception,我需要在主要名称空间“mysqli_sql_exception”中抛出\