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

标签 php mysql

我通过使用 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/

相关文章:

javascript - 如何向 CSS 选择器添加通配符?

c# - Mysql 查询 datagridview C#

MySQL LEFT JOIN 在连接表上使用 MAX 和 GROUP BY 并获取与一列的 MAX 值相对应的其他列?

mysqldump 行超时问题

mysql - MySql 中基于 IF EXISTS 的条件 SELECT

mysql - 帮我优化一个属性太多的 ActiveRecord 对象

php - HasMany() 在 Laravel 中无法用于获取关系明智的记录

php - laravel 的多重授权

php - 具有以下同级的强元素后的 xpath 文本不起作用

php - 一条语句中的多个 SQL 查询