如何捕获mysqli_connect()的警告 [英] how to catch warnings for mysqli_connect()

查看:79
本文介绍了如何捕获mysqli_connect()的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何捕获mysqli_connect()

示例

if (!$connection2 = mysqli_connect($host, $username, $password, $name)){
    die('MySQL ERROR: ' . mysql_error()); 
}

OR

$connection2 = mysqli_connect($host, $username, $password, $name);
if (!$connection2){
    die('MySQL ERROR: ' . mysql_error()); 
}

因此,我正在尝试通过在其中放入错误的主机,用户名等来测试mysqli_connect()方法.但是当我在其中输入错误的变量时(例如,我输入了hostlocal而不是localhost).它给了我这个错误

So, i am trying to test mysqli_connect() method by putting wrong host, username etc in it. but when i put the wrong variables in it such as i put hostlocal instead of localhost. it gives me this error

警告:mysqli_connect():php_network_getaddresses:getaddrinfo失败:
尚无此类主机.

Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed:
No such host is known.

我知道我将错误的主机放入其中,但是我想以适当的方式捕获错误.像这样的体面消息将是完美的:

I know i am putting the wrong host in it but i want to catch the errors in an appropriate way. a decent message would be perfect just like:

很遗憾,您输入的用于连接的详细信息不正确!

Unfortunately, the details you entered for connection are incorrect!

所以,我如何获得此消息而不是警告,谢谢

So, how do i get this message instead of the warning, Thanks

推荐答案

对于一个,mysql_error()不会给您任何东西.它是另一个库mysql_的一部分,该库不与新的和改进的mysqli_互换.话虽如此,您可以将MySQLi设置为引发异常,然后可以捕获该异常.

For one, mysql_error() won't give you anything. It's a part of another library, mysql_, which doesn't interchange with the new and improved mysqli_. That being said, you can set MySQLi to throw exceptions, which you then can catch.

如果启用了错误日志记录,则仍将记录警告(应如此!).但是,如果连接失败,您可以捕获该错误并在页面上显示一些对用户更友好的内容.您永远不要向实际的项目显示实际的错误消息-在开发中可以正常运行,但在任何实际的环境中均绝对.

The warnings would still be logged, if error-logging is enabled (as it should!). But you can catch the error and display something more user-friendly to the page if the connection fails. You should never display actual error messages to a live project - in development its fine, but never on any live environment.

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); // Set MySQLi to throw exceptions 

try {
    $connection2 = mysqli_connect($host, $username, $password, $name);
} catch (mysqli_sql_exception $e) {
    // $e is the exception, you can use it as you wish 
    die("Unfortunately, the details you entered for connection are incorrect!");
}

这将记录与您得到的日志相同的警告,

This would however log the same warning as what you're getting to the log,

php_network_getaddresses:getaddrinfo失败:名称或服务未知

php_network_getaddresses: getaddrinfo failed: Name or service not known

尽管它不会显示在页面上.

although it won't be displayed to the page.

您可以 通过添加@来抑制警告(因此不会被记录),但是我不建议您这样做,除非您明确实现自己的错误处理/报告.错误应记录.

You can suppress the warning (so it won't be logged) by adding @, but I don't recommend it unless you explicitly implement your own error-handling/reporting. Errors should be logged.

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); // Set MySQLi to throw exceptions 

try {
    $connection2 = @mysqli_connect($host, $username, $password, $name);
} catch (mysqli_sql_exception $e) {
    error_log($e->getMessage()); // Log the error manually
    die("Unfortunately, the details you entered for connection are incorrect!");
}

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆