search

Home  >  Q&A  >  body text

Close PDO connection

This is just a fairly simple question about PDO compared to MySQLi.

Using MySQLi, to close the connection you can do the following:

$this->connection->close();

However, when using PDO, it states that you open the connection using:

$this->connection = new PDO();

But to close the connection, set it to null.

$this->connection = null;

Is this correct? Will this actually release the PDO connection? (I know it is like this because it is set to null.) What I mean is, with MySQLi, you have to call a function (close) to close the connection. Does PDO disconnect as easily as = null? Or is there a function to close the connection?

P粉041758700P粉041758700406 days ago686

reply all(1)I'll reply

  • P粉448346289

    P粉4483462892023-10-15 16:05:31

    You are correct according to the documentation (http://php.net/manual/en/pdo.connections .php):

    Please note that if a PDO object is initialized as a persistent connection, it will not automatically close the connection.

    reply
    0
  • Cancelreply