So, I’m getting the following error:
Error Number: 2006
MySQL server has gone away
I can all but guarantee this is because the script takes a bazillion minutes to run, and then more queries are run in the middle of the script. Considering this is an admin-only, huge file-writing script, this is to be expected. (and is perfectly acceptable) I think the MySQL connection is getting closed because PHP / MySQL / something isn’t holding it open. However, I can’t for the life of me figure out how to stop this timeout from occurring!
I have the following at the top of my PHP script, but it doesn’t appear to be helping.
ini_set('default_socket_timeout', -1);
ini_set('max_execution_time', -1);
ini_set('memory_limit', -1);
ini_set('mysql.connect_timeout', -1);
Any idea how I can fix this problem? Thanks!
I think a viable option would be to
mysql_connecteverytime before you callmysql_query. This ensures that there will be a live connection before every query.But a much better approach is to first check for the connection status, and only then reconnect if necessary.
mysql_pingis useful for checking the connection status, so you can use this to check if re-connection is required. Example code present in @galador’s answer.Thanks @diolemo and @galador.