DB Connections have a timeout which will cause this error if you try to send a query sometime after opening the connection. The usual scenario is:
- Open DB connection
- Fetch some data from DB
- Do stuff, e.g. send emails (takes time longer than DB connection timeout)
- Query DB using same connection
- Error: MySQL server has gone away
So - what's the solution? You could simply increase the timeout, but that's ugly and could cause problems when traffic to your site increases. The best solution would be to close your DB connection and then re-open it like this:
- Open DB connection
- Fetch some data from DB
- Close DB connection
- Do stuff, e.g. send emails (takes time longer than DB connection timeout)
- Open new DB connection
- Query DB using same connection
- Close DB connection
Here's more information: http://dev.mysql.com/doc/refman/5.0/en/gone-away.html