gpt4 book ai didi

php - PDO 关闭连接

转载 作者:IT老高 更新时间:2023-10-28 11:50:10 26 4
gpt4 key购买 nike

与 MySQLi 相比,PDO 只是一个相当简单的问题。

使用 MySQLi,您可以关闭连接:

$this->connection->close();

但是,对于 PDO,它声明您使用以下方式打开连接:

$this->connection = new PDO();

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

$this->connection = null;

这是正确的吗?这真的会释放 PDO 连接吗? (我知道它设置为 null。)我的意思是使用 MySQLi 你必须调用一个函数 (close) 来关闭连接。 PDO 是否像 = null 一样容易断开连接?还是有关闭连接的功能?

最佳答案

根据文档,您是正确的 (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 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 对象初始化为持久连接,它不会自动关闭连接。

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com