Replace mysql_query with query function for consistent error checking and to facilitate debugging