Search code examples
phpmysqldisconnect

"MySQL server has gone away" after a few minutes


I have 2 tables in a query designed to display/email the saved search info of users : users & wishlist.

The search info contains book titles among other things.

I have a first query designed to put into an array all the users that have records in the wishlist table.

Then using a foreach loop, via a second query, take each book title and run a search at various sites and save that information to screen or to an email.

The first run through takes about 3 minutes for the first user's information to be assembled and yet, after this info is displayed, I see the dreaded "MySQL server has gone away".

According to http://dev.mysql.com/doc/refman/5.0/en/gone-away.html - this shouldn't happen so quickly.

The code for the second query is simple :

SELECT * FROM wishlist WHERE uid = " . $sendtouser . " ORDER BY wishid

After the information is compiled for that user, I have the following to close the loop and end the connection.

} while ($row_rsWishDetails = mysql_fetch_assoc($rsWishDetails));
mysql_free_result($rsWishDetails);

I have tried some other suggestions I have found on the web including :

ini_set('max_execution_time', 22222);
ini_set('mysql.connect_timeout', 500);
ini_set('default_socket_timeout', 600);

But nothing is fixing the problem.

Any ideas?

Thanks very much.


Solution

  • With long running scripts, you need to make sure that connection is still open before running any queries. The best way to do so is to use something like mysql_ping() before your query.

    Also, the mysql is rather old and inefficient. You should try to upgrade to mysqli.