我们是否应该手动检查mysqli_connect()错误?

发布于 2025-02-10 02:01:13 字数 1338 浏览 1 评论 0 原文

mysqli_connect() >建议检查返回值并在屏幕上显示错误消息。

$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式的构造函数,这是建议的:

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

堆栈溢出上的某些用户甚至使用 mysqli_error($ conn)

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

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

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

错误:无法连接到mySQL。调试Errno:1045调试 错误:访问用户'my_user'@'localhost'(使用密码: 是的)

因为您可以看到错误消息显示两次!手册“调试”实际上提供了更少的信息。

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

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;
}

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);
}

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:

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

污味仙女 2025-02-17 02:01:13

切勿手动显示连接错误!

如果Mysqli无法打开与MySQL的连接,则会产生警告。此警告告诉您您需要知道的所有信息,包括错误代码,错误消息以及代码发生的位置。手动检查错误不会给您更多信息。

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

如果错误日志中没有警告,则可能意味着您的PHP有错误报告沉默(完全或仅仅是警告)。检查您的PHP配置。
生产环境中,应维护这些设置:

  • error_reporting 必须是 e_all
  • log_errors 必须是 on < /code>
  • display_errors 必须是 off

开发环境中应维护这些设置:

  • error_reporting << /code>必须是 e_all
  • log_errors 必须是
  • display_errors 必须是 on

您在错误消息中看到的数据库用户名和密码已向最终用户揭示。这是您不想向任何人展示的敏感信息。实际上,普通用户不了解此隐秘消息。这就是为什么 display_errors 必须在生产环境中始终关闭。记录服务器上的错误是安全的。

警告与例外

警告不会停止脚本。如果发出警告,脚本将继续执行,直到遇到致命错误为止。在大多数情况下,您想提出一个例外以停止脚本。 请勿使用 die/exit 如果无法建立MySqli连接,则应抛出一个例外,如果未经处理,它将起泡并在脚本中停止脚本。您可以配置mysqli以自动投掷异常。这是无价的,因为所有MySQLI功能都可能出于多种原因失败,并且除非您手动检查错误,否则它们不会通知您任何问题。在打开连接之前,请使用以下行:

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

不要捕获异常,除非您真的知道该如何处理它们! https://phpdelusions.net/mysqli/mysqli/mysqli_connect#error_handling”中描述了一种可能的用例

显示任何与连接有关的问题?

编号 mysqli_error($ conn)期望MySQLI连接成功。 $ conn 必须是有效的mysqli连接,否则您会收到此错误消息:

警告:mysqli_error():无法在c:\ ...

中获取mysqli

警告:mysqli_error ( 相关错误!

相关: 我应该在调用“ mysqli_stmt_prepare”时手动检查错误

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.

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 the error log file on your server. 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.

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 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 must be E_ALL
  • log_errors must be On
  • display_errors must be On

As you can see in the error message your database username and password have been revealed to the end user. This is sensitive information that 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.

Warnings vs. Exceptions

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 invaluable 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 the connection:

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

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

Can mysqli_error() show any connection-related problems?

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:

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

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

Related: Should I manually check for errors when calling “mysqli_stmt_prepare”?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文