views:

311

answers:

2

Hey guys,

I have a script that takes several minutes to execute. The mysql connection is established at the beginning (mysql_connect), then a loop with some queries follows (mysql_query). That loop lasts very long, and after a few minutes the script throws a warning that the mysql server has gone away (Supplied argument is not a valid MySQL result resource)

Does the connection close automatically after a certain time of execution, even though queries are made and results are fetched?

+2  A: 

"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.

VolkerK
I might be reading that wrong, but it looks like the default might be 28800 seconds, which is 8 hours... if you are running something over 8 hours, php scripts might not be your answer for other reasons!
Colin Pickard
oops...."Supplied argument is not a valid MySQL result resource" - that's something completely different. Let me rewrite the whole answer....
VolkerK
Thanks for that. I have mysql_query followed directly by mysql_fetch_assoc. The script currently takes a maximum of 10 minutes.
Alex
I did now add "...or die(mysql_error())" and put the mysql_connect inside the loop, to avoid a timeout (The loop is only run about 500 times). No error is printed. Instead I get the following after some minutes: Warning: mysql_connect(): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in script.php on line xxx
Alex
The relevant part now looks like this: http://nopaste.info/1317da5191.htmlafter some time it throwsWarning: mysql_connect(): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in xxx.php on line 122Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in xxx.php on line 123Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in xxx.php on line 132Warning: mysql_error(): supplied argument is not a valid MySQL-Link resource in xxx.php on line 132I really don't know what's wong here...
Alex
`mysql_connect()` and `mysql_select_db()` also return `FALSE` in case of an error -> `$db=mysql_connect(...) or die(mysql_error()); mysql_select_db(...,$db) or die(mysql_error());` Is there a reason for (re-)connecting to the database in each iteration of the for-loop?
VolkerK
A: 

you're probably hitting a time out set by php
Max Execution Time
Set Time Limit

nelsonslament
I don't - the max execution time is set to 1 hour and the script keeps running upon the mysql errors.
Alex
Both of these are related to the maximum script execution time, the OP is having difficulties with mySQL timeouts, the script is still executing when the connection closes.
Neil Aitken