我们是否应该手动检查mysqli_connect()错误? [英] Should we ever check for mysqli_connect() errors manually?

查看:91
本文介绍了我们是否应该手动检查mysqli_connect()错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mysqli_connect() 的PHP手册建议检查返回值并在屏幕上显示错误消息.

The PHP manual for mysqli_connect() suggests checking for the return value and displaying the error messages on the screen.

$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

对于OOP风格的构造函数,类似的建议:

Similarly for OOP-style constructor this is suggested:

$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error);
}

Stack Overflow上的某些用户甚至使用了带有mysqli_error($conn)的代码,例如:

Some users on Stack Overflow even used code with mysqli_error($conn) such as this:

$conn = mysqli_connect('localhost', 'a', 'a');
if (!$con) {
    die('Could not connect: ' . mysqli_error($conn));
}

但是,在过去的几周中,我一直在问自己一个问题,为什么我需要这样做?第一个示例的输出是:

However, in the past few weeks I have been asking myself a question, why would I need to do that? The output of the first example is:

警告:mysqli_connect():(HY000/1045):拒绝用户访问 的"my_user" @"localhost"(使用密码:是) 第4行上的C:\ xampp \ ... \ mysqli.php

Warning: mysqli_connect(): (HY000/1045): Access denied for user 'my_user'@'localhost' (using password: YES) in C:\xampp\...\mysqli.php on line 4

错误:无法连接到MySQL.调试errno:1045调试 错误:用户'my_user'@'localhost'的访问被拒绝(使用密码: 是的)

Error: Unable to connect to MySQL. Debugging errno: 1045 Debugging error: Access denied for user 'my_user'@'localhost' (using password: YES)

如您所见,错误消息显示了两次!手册调试"实际上提供的信息较少.

As you can see the error message is displayed twice! The manual "debugging" actually provides less information.

我们应该手动检查连接错误吗?通过这种方式,我们会比从自动警告中获得更多的信息吗?这是推荐的做法吗?

Should we ever manually check for connection errors? Would we ever get more information this way than from the automatic warning? Is this the recommended practice?

推荐答案

从不手动显示连接错误!

如果无法打开与MySQL的连接,MySQLi将生成警告.该警告告诉您所有您需要知道的信息,包括错误代码,错误消息以及代码在其中发生的位置.手动检查错误不会为您提供更多信息.

Never display connection errors manually!

MySQLi will generate a warning if it is unable to open the connection to MySQL. This warning tells you all you need to know including the error code, error message, and the place in the code where it happened. Checking for the error manually will not give you any more information.

如果您看不到警告并且无法创建连接,则可能意味着您的PHP未配置为显示它们.在这种情况下,必须检查服务器日志中的error_log文件.如果您不知道它在哪里,请使用 phpinfo() 来获取该信息并搜索error_log.它会告诉您错误日志文件的位置.

If you can't see the warning and your connection cannot be created, it might mean that your PHP is not configured to show them. In that case, you must check your server logs for the error_log file. If you do not know where that is, use phpinfo() to get that information and search for error_log. It will tell you where the error log file is located.

如果错误日志中没有警告,则可能意味着您的PHP错误报告已静音(完全警告或仅警告).检查您的PHP配置.
生产环境中,应保留以下设置:

If there is no warning in the error logs, it could mean that your PHP has error reporting silenced (either completely or just warnings). Check your PHP configuration.
In the production environment these settings should be maintained:

  • error_reporting必须为E_ALL
  • log_errors必须为On
  • display_errors必须为 Off
  • error_reporting must be E_ALL
  • log_errors must be On
  • display_errors must be Off

开发环境中,应保留以下设置:

In the development environment these settings should be maintained:

  • error_reporting必须为E_ALL
  • log_errors必须为On
  • display_errors必须为 On
  • error_reporting must be E_ALL
  • log_errors must be On
  • display_errors must be On

您会在错误消息中看到,您的数据库用户名和密码已透露给最终用户.这些是敏感信息,您不想显示给任何人.实际上,普通用户不会理解该隐秘消息.这就是在生产环境中必须始终关闭display_errors的原因.在服务器上记录错误是安全的.

As you can see in the error message your database username and password has been revealed to the end user. These are sensitive information which you do not want to show anyone. In fact a normal user would not understand this cryptic message. This is why display_errors must always be switched off in the production environment. Logging the errors on the server is safe.

警告不会停止脚本.如果发出警告,脚本将继续执行直到遇到致命错误.在大多数情况下,您可能想抛出一个异常来停止脚本. 请勿使用die/exit.如果无法建立mysqli连接,则应引发异常,如果未处理该异常,它将冒出气泡并以致命错误停止脚本.您可以将mysqli配置为自动引发异常.这非常有用,因为所有mysqli函数都可能由于多种原因而失败,除非您手动检查其中的每一个错误,否则它们不会通知您任何问题.打开连接之前,请使用以下行:

Warnings do not stop the script. If a warning is emitted the script will keep on executing until it encounters a fatal error. In most cases you would want to throw an exception to stop the script. Do not use die/exit! If mysqli connection cannot be made, an exception should be thrown and if it is unhandled it will bubble up and stop the script with a fatal error. You can configure mysqli to throw exceptions automatically. This is very useful, because all mysqli functions can fail for many reasons and they will not inform you about any problems unless you check for errors manually every single one of them. Use the following line before opening connection:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');

除非您真的知道如何处理异常,否则不要捕获异常! 如何使用mysqli正确连接

Do not catch the exceptions unless you really know what to do with them! One possible use case is described in How to connect properly using mysqli

不. mysqli_error($conn)期望mysqli连接成功. $conn必须是有效的mysqli连接,否则您将收到以下错误消息:

No. mysqli_error($conn) expects that the mysqli connection was successful. $conn must be a valid mysqli connection otherwise you would get this error message:

警告:mysqli_error():无法在C:\ ...中获取mysqli.

Warning: mysqli_error(): Couldn't fetch mysqli in C:\...

$conn->errormysqli_error($conn)均不能显示任何与连接有关的错误!

Neither $conn->error nor mysqli_error($conn) can display any connection related errors!

这篇关于我们是否应该手动检查mysqli_connect()错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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