gpt4 book ai didi

php - MySQLi,PHP-尝试{…}捕获(…){…}不起作用

转载 作者:行者123 更新时间:2023-12-03 08:51:13 27 4
gpt4 key购买 nike

我正在尝试使用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);
}
}

Fatal error: Uncaught mysqli_sql_exception: Access denied for user 'myinvaliduser'@'10.241.84.65' (using password: YES) in /oxygen-v2/php-bin/datadrivers/mysqlz/mysqlz.php on line 77

最佳答案

\ó/
我确定错误只是愚蠢的事情!

解决了替换这段代码的问题:

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

通过此代码:
} catch (\mysqli_sql_exception $e) {
$this->MyErrorFunction($e);
}

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

关于php - MySQLi,PHP-尝试{…}捕获(…){…}不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40308645/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com