php PDO 关闭连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18277233/
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
PDO closing connection
提问by Liam Sorsby
Just a rather simple question with regards to PDO compared to MySQLi.
与 MySQLi 相比,PDO 只是一个相当简单的问题。
With MySQLi, to close the connection you could do:
使用 MySQLi,要关闭连接,您可以执行以下操作:
$this->connection->close();
However with PDO it states you open the connection using:
但是,对于 PDO,它声明您使用以下方法打开连接:
$this->connection = new PDO();
but to close the connection you set it to null
.
但要关闭连接,您将其设置为null
.
$this->connection = null;
Is this correct and will this actually free the PDO connection? (I know it does as it is set to null
.) I mean with MySQLi you have to call a function (close
) to close the connection. Is PDO as easy as = null
to disconnect? Or is there a function to close the connection?
这是正确的,这实际上会释放 PDO 连接吗?(我知道它设置为null
。)我的意思是对于 MySQLi,您必须调用函数 ( close
) 来关闭连接。PDO 和= null
断开连接一样容易吗?或者有没有关闭连接的功能?
回答by Kieran
According to documentation you're correct (http://php.net/manual/en/pdo.connections.php):
根据文档,您是正确的(http://php.net/manual/en/pdo.connections.php):
The connection remains active for the lifetime of that PDO object. To close the connection, you need to destroy the objectby 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 对象的生命周期内保持活动状态。要关闭连接,您需要通过确保删除对它的所有剩余引用来销毁该对象——您可以通过将 NULL 分配给保存该对象的变量来完成此操作。如果您不明确这样做,PHP 将在您的脚本结束时自动关闭连接。
Note that if you initialise the PDO object as a persistent connection it will not automatically close the connection.
请注意,如果您将 PDO 对象初始化为持久连接,它不会自动关闭连接。
回答by himanshu dholakia
$conn=new PDO("mysql:host=$host;dbname=$dbname",$user,$pass);
// If this is your connection then you have to assign null
// to your connection variable as follows:
$conn=null;
// By this way you can close connection in PDO.
回答by Fil
I created a derived class to have a more self-documenting instruction instead of "$conn=null;".
我创建了一个派生类,以具有更多的自我记录指令,而不是“$conn=null;”。
class CMyPDO extends PDO {
public function __construct($dsn, $username = null, $password = null, array $options = null) {
parent::__construct($dsn, $username, $password, $options);
}
static function getNewConnection() {
$conn=null;
try {
$conn = new CMyPDO("mysql:host=$host;dbname=$dbname",$user,$pass);
}
catch (PDOException $exc) {
echo $exc->getMessage();
}
return $conn;
}
static function closeConnection(&$conn) {
$conn=null;
}
}
So i can call my code between:
所以我可以在以下之间调用我的代码:
$conn=CMyPDO::getNewConnection();
// my code
CMyPDO::closeConnection($conn);
回答by Jdahern
Its more than just setting the connection to null. That may be what the documentation says, but that is not the truth for mysql. The connection will stay around for a bit longer (Ive heard 60s, but never tested it)
它不仅仅是将连接设置为空。这可能是文档所说的,但这不是 mysql 的真相。连接会保持一段时间(我听说过 60 年代,但从未测试过)
If you want to here the full explanation see this comment on the connections https://www.php.net/manual/en/pdo.connections.php#114822
如果您想在这里获得完整的解释,请参阅有关连接https://www.php.net/manual/en/pdo.connections.php#114822 的此评论
To force the close the connection you have to do something like
要强制关闭连接,您必须执行以下操作
$this->connection = new PDO();
$this->connection->query('KILL CONNECTION_ID()');
$this->connection = null;
回答by yo lo
<?php if(!class_exists('PDO2')) {
class PDO2 {
private static $_instance;
public static function getInstance() {
if (!isset(self::$_instance)) {
try {
self::$_instance = new PDO(
'mysql:host=***;dbname=***',
'***',
'***',
array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4 COLLATE utf8mb4_general_ci",
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
)
);
} catch (PDOException $e) {
throw new PDOException($e->getMessage(), (int) $e->getCode());
}
}
return self::$_instance;
}
public static function closeInstance() {
return self::$_instance = null;
}
}
}
$req = PDO2::getInstance()->prepare('SELECT * FROM table');
$req->execute();
$count = $req->rowCount();
$results = $req->fetchAll(PDO::FETCH_ASSOC);
$req->closeCursor();
// Do other requests maybe
// And close connection
PDO2::closeInstance();
// print output
Full example, with custom class PDO2.
完整示例,带有自定义类 PDO2。