Link to home
Create AccountLog in
Avatar of HelpNearMe
HelpNearMeFlag for Afghanistan

asked on

Error: "MySQL server has gone away"

Hi Experts,

I have written a bit of PHP script to query a DB and loop through 1200+ rows and build a couple multidimensional arrays.  The trouble is that the queries & loops etc take so long that it appears the server connection closes.

When I lower the loop count limit to 3 or for instead of 1200 I don't get any errors and everything is great.  So it seems to be related to the time it takes to complete the script.

It's a godaddy MySQL server so I doubt that I have control over reconnect & timeout parameters.  Is there anything in the script I can do to hold the connection open or should I open and close the connection with every loop iteration?

I am getting this error later in the script when I try an insert after the big time consuming loop: MySQL server has gone away.
.

Thanks,

HNM
Avatar of arnold
arnold
Flag of United States of America image

The error means that the connection you had defined/opened is no longer valid.  There is a test to make sure the connection is till present before even trying to pass a transaction.

The other option which is not available to you is to increase the idle connection timeout.  Or during the loop you check/send a noop through the connection.
Avatar of HelpNearMe

ASKER

Thanks arnold,

What is the test?

HNM
http://www.wallpaperama.com/forums/simple-php-mysql-connection-test-script-example-t5702.html


Another approach is to generate the request, close the connection if it is not needed and then reconnect when you need to interact with the database.
http://php.net/manual/en/function.mysql-ping.php
ASKER CERTIFIED SOLUTION
Avatar of arnold
arnold
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of madunix
madunix

look @ http://dev.mysql.com/doc/refman/5.0/en/gone-away.html 
Tryincreasing the "wait_timeout"  variable
madunix:  I can use google too.. .as I said in my question I don't have access to the server settings.
Thanks arnold,

I found a typo that caused it.  I closed the connection then tried to select the DB.  Seems to work fine now.  

HNM