"Supplied argument is not a valid MySQL result resource" means that a mysql_...() function expects you to pass a result resource but you did pass something else, e.g.
$result = false;
$row = mysql_fetch_array($result, MYSQL_ASSOC);
// => Supplied argument is not a valid MySQL result resource
So, why would $result be false
? Amost always it's because the previous query failed and there is no error handling in the script. Each and every query can fail, there's nothing you can do to prevent that from ever happening. Therefore you always need some kind of error handling so that your script doesn't try to process the result when there is none (but an error condition). The simplest error handling is to let the script stop whenever an error occurred.
$sql = 'SELECT x,y,z FROM ...';
$result = mysql_query($sql, $mysql) or die(mysql_error());
mysql_query() returns false
if there was an error. In that case the statement after or
is executed, i.e. if the query failed php will print MySQL's last error message and then quit.
You might want to implement a somewhat more advanced error handling routine though...
E.g. when an PDO object is set to PDO::ERRMODE_EXCEPTION an exception is thrown whenever an error occurs. It's a bit harder to miss that compared to a (simple) return value.
edit: this is a complete rewrite of the original answer. The first suggestion was to look at the value of wait_timeout.