Thank you so much! I was getting ready to make a php script to do all that, but you pointed me in the right way - thanks!

Why are you using mySql? Myself, I need it to a) generate KML for only certain types of data (namely buildings) and b) ensuring that the polygons generated match OSM’s data (there are few (or no) reliable OSM to KML translators out there - all I’ve tried dropped multipolygon info (polygons with holes, for example). Cheers!