I am trying to write a query to a file for debugging. The file is in database/execute.php. The file I want to write to is database/queries.php.
Realise this is pretty old now, but there's no need to manually write queries to a file like this. MySQL has logging support built in, you just need to enable it within your dev environment.
Take a look at the documentation for the 'general query log':
http://dev.mysql.com/doc/refman/5.1/en/query-log.html