In most case there is no reason to use mysql_close() combined with persistent connections, but below comes some explanation.
First of all, you should make the difference between a resource (a PHP variable, internally: a zval) and a connection (e.g. a TCP or socket connection aka a "link").
When you call mysql_connect(), a new connection is created every time and a new resource is returned. Multiple calls to mysql_connect() in the same script will create multiple connections and a 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 a new resource is also returned every time. Multiple calls to mysql_pconnect() will return different resources which all point to the same connection.
mysql_close() will have an action on both the connection and the resource:
- If the connection pointed by the resource is not a persistent one, the connection is closed.
- The resource is destroyed in every case.
It does mean that using mysql_close() on a persistent connection, you have no way to use the connection anymore even if this one is still open, you can debug this 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 enabled in your error_reporting, you would have seen 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 again a new resource which will point to it:
$connp = mysql_pconnect("localhost", "root", "password");
Using mysql_thread_id() on the resource will give you the MySQL connection identifier so that you can ensure mysql_close() does not close a persistent connection but only destroy 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 output something like:
61
62
62