PDO 关闭连接 [英] PDO closing connection

查看:52
本文介绍了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天全站免登陆