9

I know this question has been asked numerous times but I've tried everything.

I have a PHP script that iterates through thousands of images, performs resizing if necessary and then inserts into the database.

I'm getting this error message:

Warning: mysql_query() [function.mysql-query]: MySQL server has gone away in C:\Utilities\server\htdocs\newGen\index.php on line 105

Warning: mysql_query() [function.mysql-query]: Error reading result set's header in C:\Utilities\server\htdocs\newGen\index.php on line 105

I'm using Xampplite and so some parts of the php.ini are missing that others have used to solve the problem because they're not using the lite version.

I'm certain the problem is not timeouts. While the script will run for several minutes it skips over images that have already been processed and so I quickly get the error message within about 10 seconds. Maybe this has something to do with max table size? I can't find anything in my php.ini that controls this.

All help is greatly appreciated, I've been going at this for hours now.

EDIT:

Here is the code at the 105 mark, it's the query:

        // Add tNail and image to db
        mysql_query("INSERT INTO gallery values('','$hash','$thumbBlob','$imageBlob','$type','','0','0.0')");
    //  $q->execute();
        print "Successfully processed $fileName<br>";

As you can see by the comments I tried PDO with this and got the same error, foolishly I thought going back to mysql_ commands would help.

If it's of any help the script complains at the same image every time, yet each image is only ever a few tens of kilobytes and a couple hundred at the most.

5
  • Show us the code around line 105?
    – Ghost
    Jul 20, 2012 at 16:26
  • Blob Blob Blob -> large data probably -> maybe above the package size?
    – hakre
    Jul 20, 2012 at 16:30
  • 2
    Please, don't use mysql_* functions for new code. They are no longer maintained and the community has begun the deprecation process. See the red box? Instead you should learn about prepared statements and use either PDO or MySQLi. If you can't decide, this article will help to choose. If you care to learn, here is good PDO tutorial. Jul 20, 2012 at 16:31
  • That's the thing, I can't find packet size anywhere in my php.ini :/ I'm using xampplite
    – Lee
    Jul 20, 2012 at 16:31
  • Truth, I already tried PDO. I went back to mysql_ in case it fixed the issue, which it hasn't. As soon as this is sorted I'm going back to PDO.
    – Lee
    Jul 20, 2012 at 16:31

3 Answers 3

30

Check the value of "max_allowed_packet" in your my.cnf MySQL config file.

1
  • 1
    THANK YOU! I was looking through php.ini for ages when it was the my.cfg I should have looked at!
    – Lee
    Jul 20, 2012 at 16:42
1

This happens because the PHP script took too long to process whatever it was processing, and the connection to the server timed out. You have a few solutions:

  • Check to make sure the connection is still up, and reconnect if needed (mysql_connect has built-in functionality to do that for you)
  • Use mysql_pconnect (but remember to close the connection at the end because it won't close for you)
  • Improve the running time of your script to avoid the timeout.
2
  • I tried mysql_pconnect() and get the same problem. All the timeouts in the php.ini are -1 or high numbers, so I'm sure it's not a time issue.
    – Lee
    Jul 20, 2012 at 16:30
  • @Kolink I was just going to write the same thing. In the past this has happen to me. If my script didn't hit the mysql server for sometime. Mysql would close the connection. I solved this by checking if mysql was still connected. Have you possibly tried mysqltuner to make sure nothing is wrong on that side? Jul 20, 2012 at 16:38
1

you can use mysql_ping() to see if the connection still connected, if not you can try to reconnnect, or make an error message telling you how far the script had gone before the connection was lost.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.