Should SQL connections opened with PDO in PHP be closed?

When I open a MySQL connection in PHP using the built-in MySQL MySQL functions, I do the following:

$link = mysql_connect($servername, $username, $password); mysql_select_db($dbname); //queries etcetera mysql_close($link); 

When I open a connection to PDO, it looks like this:

 $link = new PDO("mysql:dbname=$dbname;host=$servername",$username,$password); //prepare statements, perform queries 

Should I explicitly close the connection, as I do with mysql_connect() and mysql_close() ? If not, how does PHP know when I ended up with my connection?

TIA.

+61
sql php mysql pdo
Jun 25 '09 at 22:57
source share
6 answers

Use $link = null so that the PDO knows that it can close the connection.

PHP: PDO Connections and Connection Management

After successfully connecting to the database, an instance of the PDO class is returned to your script. The connection remains active for the lifetime of this PDO. To close the connection, you need to destroy the object, making sure that all other references to it are deleted - you do this by assigning NULL to the variable that contains the object. If you do not do this explicitly, PHP will automatically close the connection when your script ends.

+76
Jun 25 '09 at 23:01
source share

When the PHP script completes execution, all connections are closed. Also, you do not need to explicitly close your connection with mysql_close() .

+11
Jun 25 '09 at 23:01
source share

PDO does not offer such a function on its own. Connections through PDOs are indirectly controlled using refcount PDOs in PHP.

But sometimes you want to close the connection in any case, regardless of how much was recalculated. Or because you cannot control it, you need it for testing purposes or similar.

You can close the Mysql connection with PDO by running an SQL query. Each user who can connect to the Mysql server is capable of KILL at least its own stream:

 /* * Close Mysql Connection (PDO) */ $pdo_mysql_close = function (PDO $connection) { $query = 'SHOW PROCESSLIST -- ' . uniqid('pdo_mysql_close ', 1); $list = $connection->query($query)->fetchAll(PDO::FETCH_ASSOC); foreach ($list as $thread) { if ($thread['Info'] === $query) { return $connection->query('KILL ' . $thread['Id']); } } return false; }; $pdo_mysql_close($conn); 

Related Mysql Documentation:

Related stack questions:

  • PHP PDO close ()? (April 2012)
+11
Jan 01 '13 at 18:34
source share

You can also limit your connections to local features. Thus, the connection is closed as soon as the function ends.

+5
Oct 28 2018-10-28
source share

Seeing clearly that an object is assigned to the PDO $link object, PHP will set this value to null as soon as the script is executed so that it is no longer an object. So you can just do:

 $link = new PDO("mysql:dbname=$dbname;host=$servername",$username,$password); //prepare statements, perform queries $link = null; 
+1
Jul 04 '13 at 21:17
source share

http://uk3.php.net/pdo

From what I am collecting, I still could not close it in the php manual, and the sample scripts that I looked quickly at never closed the connection anyway from what I could see.

0
Jun 25 '09 at 23:03
source share



All Articles