PDO 关闭连接

PDO closing connection(PDO 关闭连接)
本文介绍了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 关闭连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)