Alex
Alex

Reputation: 12433

mysql connection closes after a certain time? (PHP)

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?

Upvotes: 0

Views: 2354

Answers (2)

VolkerK
VolkerK

Reputation: 96159

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

Upvotes: 2

nelsonslament
nelsonslament

Reputation: 537

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

Upvotes: -1

Related Questions