In most cases, there is no reason to use mysql_close () in combination with persistent connections, but here is some explanation.
First of all, you must make the difference between the resource (PHP variable, internally: zval) and the connection (for example, a TCP connection or aka socket βlinkβ).
When mysql_connect () is called, a new connection is created each time, and a new resource is returned. Multiple calls to mysql_connect () in the same script will create multiple connections and the corresponding resource will be returned.
When you call mysql_pconnect (), a new connection is created only if no active persistent connection is found in the current PHP process , and the new resource is also returned every time. Multiple calls to mysql_pconnect () return different resources that all point to the same connection.
mysql_close () will have an action for both the connection and the resource :
- If the connection indicated by the resource is not permanent, the connection is closed.
- The resource is destroyed in each case.
This means that using mysql_close () in a persistent connection, you can no longer use the connection, even if it is still open, you can debug it with:
<?php $connp = mysql_pconnect("localhost", "root", "password"); mysql_close($connp); sleep(60); ?>
and looking in MySQL that the connection is still active:
SHOW PROCESSLIST;
If you had warnings included in your error_reporting, you would see a message like:
Warning: mysql_ping(): 4 is not a valid MySQL-Link resource in ... on line ...
The only way to use the previously created persistent connection is to create a new resource again that points to it:
$connp = mysql_pconnect("localhost", "root", "password");
Using mysql_thread_id () in the resource will give you the MySQL connection identifier, so you can make sure mysql_close () does not close the persistent connection, but only destroys the resource:
<?php $conn = mysql_connect("localhost", "root", "password"); echo mysql_thread_id($conn), "\n"; mysql_close($conn); $connp1 = mysql_pconnect("localhost", "root", "password"); echo mysql_thread_id($connp1), "\n"; mysql_close($connp1); $connp2 = mysql_pconnect("localhost", "root", "password"); echo mysql_thread_id($connp2), "\n"; mysql_close($connp2); ?>
This will result in:
61 62 62