Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do SQL connections opened with PDO in PHP have to be closed

Tags:

sql

php

mysql

pdo

When I open a MySQL connection in PHP with just PHP's built-in 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 with PDO, it looks like this:

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

Do I have to explicitly close the connection like I do with mysql_connect() and mysql_close()? If not, how does PHP know when I'm done with my connection?

TIA.

like image 857
benjy Avatar asked Jun 25 '09 22:06

benjy


People also ask

Do you need to close PDO connection?

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.

What is the correct syntax to close the PDO connection?

With MySQLi, to close the connection you could do: $this->connection->close(); However with PDO it states you open the connection using: $this->connection = new PDO();

How does PDO work in PHP?

PDO—PHP Data Objects—are a database access layer providing a uniform method of access to multiple databases. It doesn't account for database-specific syntax, but can allow for the process of switching databases and platforms to be fairly painless, simply by switching the connection string in many instances.


2 Answers

Use $link = null to let PDO know it can close the connection.

PHP: PDO Connections & Connection Management

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.

like image 102
DreadPirateShawn Avatar answered Oct 19 '22 21:10

DreadPirateShawn


PDO does not offer such a function on its own. Connections via PDO are indirectly managed via the PDO objects refcount in PHP.

But sometimes you want to close the connection anyway, regardless of the refcount. Either because you can not control it, need it for testing purposes or similar.

You can close the Mysql connection with PDO by running a SQL query. Every user that is able to connect to the Mysql server is able to KILL at least its own thread:

/*  * 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:

  • 13.7.5.30. SHOW PROCESSLIST Syntax
  • 13.7.6.4. KILL Syntax

Related Stackoverflow Questions:

  • PHP PDO close()? (Apr 2012)
like image 21
hakre Avatar answered Oct 19 '22 22:10

hakre