是否有必要关闭 PDO 连接
我注意到 PDO 没有关闭功能.我应该关闭连接还是 PDO 不需要?
解决方案成功连接到数据库后,PDO 类的实例将返回到您的脚本.该连接在该 PDO 对象的生命周期内保持活动状态.要关闭连接,您需要通过确保删除对它的所有剩余引用来销毁该对象——您可以通过将 NULL 分配给保存该对象的变量来完成此操作.如果您没有明确这样做,PHP 将在您的脚本结束时自动关闭连接.
http://php.net/manual/en/pdo.connections.php>
所以答案是否定的,除非出于某种原因需要在脚本执行期间显式关闭连接,否则您不需要做任何事情,在这种情况下,只需将 PDO 对象设置为 null.
I noticed there is no close function for PDO. Should I close the connection or is it unnecessary for PDO?
解决方案Upon successful connection to the database, an instance of the PDO class is returned to your script. 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.
http://php.net/manual/en/pdo.connections.php
So the answer is no, you don't need to do anything unless you need to explicitly close the connection during the script execution for whatever reason, in which case just set your PDO object to null.
相关文章