在 PHP 中使用 PDO 打开的 SQL 连接是否必须关闭
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1046614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do SQL connections opened with PDO in PHP have to be closed
提问by benjy
When I open a MySQL connection in PHP with just PHP's built-in MySQL functions, I do the following:
当我仅使用 PHP 的内置 MySQL 函数在 PHP 中打开 MySQL 连接时,我执行以下操作:
$link = mysql_connect($servername, $username, $password);
mysql_select_db($dbname);
//queries etcetera
mysql_close($link);
When I open a connection with PDO, it looks like this:
当我打开与 PDO 的连接时,它看起来像这样:
$link = new PDO("mysql:dbname=$dbname;host=$servername",$username,$password);
//prepare statements, perform queries
Do I have to explicitly close the connection like I do with mysql_connect()and mysql_close()? If not, how does PHP know when I'm done with my connection?
我是否必须像使用mysql_connect()and那样显式关闭连接mysql_close()?如果没有,PHP 如何知道我的连接何时完成?
TIA.
TIA。
回答by DreadPirateShawn
Use $link = nullto let PDO know it can close the connection.
使用$link = null让PDO知道它可以关闭连接。
PHP: PDO Connections & Connection Management
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.
成功连接到数据库后,PDO 类的实例将返回到您的脚本。该连接在该 PDO 对象的生命周期内保持活动状态。要关闭连接,您需要通过确保删除对它的所有剩余引用来销毁该对象——您可以通过将 NULL 分配给保存该对象的变量来完成此操作。如果您没有明确地这样做,PHP 将在您的脚本结束时自动关闭连接。
回答by hakre
PDO does not offer such a function on its own. Connections via PDO are indirectly managed via the PDO objects refcount in PHP.
PDO 本身不提供这样的功能。通过 PDO 的连接通过 PHP 中的 PDO 对象 refcount 间接管理。
But sometimes you want to close the connection anyway, regardless of the refcount. Either because you can not control it, need it for testing purposes or similar.
但有时您无论如何都想关闭连接,而不管引用计数如何。要么是因为您无法控制它,要么出于测试目的或类似目的需要它。
You can close the Mysqlconnection with PDOby running a SQL query. Every user that is able to connect to the Mysql server is able to KILLat least its own thread:
您可以通过运行 SQL 查询来关闭与 PDO的Mysql连接。每个能够连接到 Mysql 服务器的用户KILL至少能够连接到自己的线程:
/*
* Close Mysql Connection (PDO)
*/
$pdo_mysql_close = function (PDO $connection) {
$query = 'SHOW PROCESSLIST -- ' . uniqid('pdo_mysql_close ', 1);
$list = $connection->query($query)->fetchAll(PDO::FETCH_ASSOC);
foreach ($list as $thread) {
if ($thread['Info'] === $query) {
return $connection->query('KILL ' . $thread['Id']);
}
}
return false;
};
$pdo_mysql_close($conn);
Related Mysql Documentation:
相关的 Mysql 文档:
Related Stackoverflow Questions:
相关的 Stackoverflow 问题:
- PHP PDO close()?(Apr 2012)
- PHP PDO 关闭()?(2012 年 4 月)
回答by Vexatus
When the PHP script finishes executing, all connections are closed. Also you don't have to explicitly close your connection with mysql_close().
当 PHP 脚本完成执行时,所有连接都将关闭。此外,您不必明确关闭与mysql_close().
回答by JDelage
You can also limit your connections to within local functions. That way the connection is closed as soon as the function is completed.
您还可以将连接限制在本地函数内。这样,一旦功能完成,连接就会关闭。
回答by Rob Holland
Well seeing as the $linkfor the PDO is assigned an object, PHP would set that as null as soon as the script runs so that it's no longer an object. Therefore you could just do:
好吧,因为$linkPDO 被分配了一个对象,PHP 会在脚本运行后立即将其设置为 null,这样它就不再是一个对象。因此你可以这样做:
$link = new PDO("mysql:dbname=$dbname;host=$servername",$username,$password);
//prepare statements, perform queries
$link = null;
回答by pjau
From what i gather i could not see anyway to close it in the php manual, and examples of scripts i quickly looked at never closed the connection in anyway from what i could see.
从我收集的信息来看,无论如何我都看不到在 php 手册中关闭它,并且我快速查看的脚本示例从未从我所看到的情况下以任何方式关闭连接。

