PDO关闭连接 [英] PDO closing connection

查看:78
本文介绍了PDO关闭连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与MySQLi相比,关于PDO的问题很简单.

Just a rather simple question with regards to PDO compared to MySQLi.

使用MySQLi,可以关闭连接:

With MySQLi, to close the connection you could do:

$this->connection->close();

但是对于PDO,它指出您使用以下方法打开连接:

However with PDO it states you open the connection using:

$this->connection = new PDO();

但要关闭连接,请将其设置为null.

but to close the connection you set it to null.

$this->connection = null;

这是正确的吗,这实际上会释放PDO连接吗? (我知道它的确是设置为null的方式.)我的意思是,对于MySQLi,您必须调用一个函数(close)来关闭连接. PDO与= 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?

推荐答案

根据文档,您是正确的( http ://php.net/manual/en/pdo.connections.php ):

According to documentation you're correct (http://php.net/manual/en/pdo.connections.php):

该连接在该PDO对象的生存期内保持活动状态.到 关闭连接,您需要通过确保销毁对象 删除所有其余的引用-您可以通过分配 保存对象的变量为NULL.如果你不这样做 明确地, PHP将在您的 脚本结束.

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对象初始化为持久连接,它将不会自动关闭连接.

Note that if you initialise the PDO object as a persistent connection it will not automatically close the connection.

这篇关于PDO关闭连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆