gpt4 book ai didi

php - 在 php 脚本中不关闭 mysql 连接的缺点是什么?

转载 作者:行者123 更新时间:2023-11-29 00:11:32 27 4
gpt4 key购买 nike

我通过使用 mysqli_connect 函数建立连接来访问 php 脚本中的数据库。我观察到在脚本末尾关闭连接不是强制性的。

在为访问 php 中的 mysql 数据库而创建的连接脚本中不使用 mysqli_close() 有什么影响?

最佳答案

如果您使用的是 cgi,则无需关闭 mysql 连接,因为它们会在脚本执行结束时自动关闭。

来自documentation :

Note: The link to the server will be closed as soon as the execution of the script ends, unless it's closed earlier by explicitly calling mysql_close().

虽然关闭连接被认为是一种好的做法。

如果您自己关闭连接:

  • You have to check the value of $_connected for every single query. This means PHP has to check that the variable $_connected A) exists B) is a boolean and C) is true/false.
  • You have to call your 'disconnect' function, and function calls are one of the more expensive operations in PHP. PHP has to check that your function A) exists, B) is not private/protected and C) that you provided enough arguments to your function. It also has to create a copy of the $connection variable in the new local scope.
  • Then your 'disconnect' function will call mysql_close() which means PHP A) checks that mysql_close() exists and B) that you have provided all needed arguments to mysql_close() and C) that they are the correct type (mysql resource).

因此,如果您没有使用持久连接,您的 MySQL 连接将在页面执行结束时关闭。所以你不必为此烦恼。因此没有缺点。

关于php - 在 php 脚本中不关闭 mysql 连接的缺点是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24852765/

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