php - 是否需要关闭 PDO 连接

标签 php pdo

我注意到 PDO 没有关闭功能。我应该关闭连接还是 PDO 不需要?

最佳答案

Upon successful connection to the database, an instance of the PDO class is returned to your script. The connection remains active for the lifetime of that PDO object. To close the connection, you need to destroy the object by ensuring that all remaining references to it are deleted--you do this by assigning NULL to the variable that holds the object. If you don't do this explicitly, PHP will automatically close the connection when your script ends.

http://php.net/manual/en/pdo.connections.php

所以答案是否定的,除非出于任何原因需要在脚本执行期间显式关闭连接,否则您不需要执行任何操作,在这种情况下,只需将您的 PDO 对象设置为 null。

关于php - 是否需要关闭 PDO 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15444748/

相关文章:

php - 未定义的属性,PDOException 错误

PHP插入查询重新调整成功但没有将数据插入表

PHP 关闭 mysqli 与数据库的连接错误

php - 如何查看从MySQL数据库取回的图像

php - 使用变量作为数据库名称使用 php/PDO 创建新数据库

php - 我的 PDO 查询是否安全,不会受到 SQL 注入(inject)的影响

php - 如何删除使用 PDO 插入 MySQL 表的最后一条记录?

php - 如何通过 PHP Web 服务器 URL 传递数组?

php - 跨表查询

PHP - 如何在没有 key 的情况下读取发布数据?