Since posting the original copy of this message -- which, in 10 hours, never appeared on this newsgroup -- I upgraded from PHP version 4.3.4 to 5.0.1. Unfortunately, the same exact problem persists. I'm starting to believe this is a bug in PHP's MySQL memory management functions.
Original Post: ------------- I've set up several tests to try to find some way to get the mysql_close() function to work without throwing warning: Warning: mysql_close(): ## is not a valid MySQL-Link resource (where ## is some number like 18 or 39) The most robust form of these tests involves a database connection management class. The entirety of the class code is too large to post here, but these two methods should illustrate the management of the MySQL connection handle (there is a class property called hndConnection): ===========================CODE=============================== function openDatabase() { $bvStatus = false; // Close any open connection. $this->closeDatabase(); // Attempt to establish a connection to the target database host. $this->hndConnection = mysql_connect($this->DATA_HOSTNAME, $this->DATA_USER, $this->DATA_PASSWORD); if (0 < $this->hndConnection) { // Connection succeeded; access the target database; if (mysql_select_db($this->DATA_DATABASE, $this->hndConnection)) { $this->bConnected = true; $bvStatus = true; } else { // Database selection failed; purge the database host connection. $this->closeDatabase(); } } else { // Connection failed; purge the handle. $this->hndConnection = NULL; } return ($bvStatus); } function closeDatabase() { $bvStatus = false; if ($this->isConnected()) { // Connected; try to close the connection. if (mysql_close($this->hndConnection)) { // Close attempt succeeded. $this->bConnected = false; $this->hndConnection = NULL; $bvStatus = true; } else { // Close attempt failed. $bvStatus = false; } } else { // No open connection; close was successful. Be sure the connection handle is purged. $bvStatus = true; $this->hndConnection = NULL; } return ($bvStatus); } ===========================/CODE=============================== As you can imagine, openDatabase() works just fine. I am able to use the open MySQL connection in all its facets. However, when it comes time to close the connection, closeDatabase() fails with the above-stated PHP Warning. As for mysql_free_result(), I get the same type of PHP Warning when I try to run a query and dump the results. For example, I get another PHP Warning complaining about "not a valid MySQL-Link resource" when I try to ($hndResult brings the same Warning from mysql_free_result(), even though -- as you can see -- it is clearly being used just like the PHP documentation advises): ===========================CODE=============================== function runQuery($szQuery) { $ivRecordCount = -1; $bvSelectQuery = false; $hndResult = 0; // Identify whether the current query is a SELECT statement (or empty). if (0 < strlen($szQuery)) { $bvSelectQuery = stristr(substr(ltrim($szQuery), 0, 6), "SELECT"); // Open the database connection if it is closed; proceed only with a valid connection. if ($this->ensureConnection()) { // Run the query. $hndResult = mysql_query($szQuery, $this->getConnectionHandle()); // Check to see whether the query succeeded. if ($hndResult) { if ($bvSelectQuery) { $ivRecordCount = mysql_num_rows($hndResult); } else { $ivRecordCount = mysql_affected_rows($this->getConnectionHandle()); } // Purge memory used by this query. mysql_free_result($hndResult); } } } return ($ivRecordCount); } ===========================/CODE=============================== As you can see in this later method, the result handle is created and used only within several lines of code of each other, yet, the resource is already invalid by the time I try to use it. What's up? Naturally, the concept of these code samples came right out of PHP's documentation, yet they don't work. Can anyone illustrate a workaround to use these two mysql_* functions successfully? Incidentally, please don't waste time preaching about "these functions not needed because all resources are closed at the end of the script's life". My programming philosophy differs from such a lazy approach, and I will not humor it. Please limit replies to constructive workarounds. Thanks! -- William Kimball, Jr. "Programming is an art-form that fights back!" -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php