The error message 'Maximum execution time of 30 seconds exceeded' doesn't need to be related to MySQL, as it just says that your script's running time is longer than allowed.
Do you get, apart from that, any error message that indicates a relation to MySQL?
On first look your code looks okay, could it be that your query just consumes much time?