Is it necessary to close PDO connections

后端 未结 3 1578
梦毁少年i
梦毁少年i 2020-12-10 11:07

I noticed there is no close function for PDO. Should I close the connection or is it unnecessary for PDO?

3条回答
  •  我在风中等你
    2020-12-10 11:40

    From the PDO's connection page

    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.


    EDIT

    I'd rather use persistent connection. Though, it's a good practice to close all connections at the end of the script.

提交回复
热议问题