All that means is that sometime during the query (possibly because it takes a long time) and the MySQL server cuts the connection.
You could try using a persistent connection using [man]mysql_pconnect/man or you could look in your MySQL setup files to see if there's something there. Other than that, it's pretty much not really a coding issue, more of a server setup issue.