But it's mandatory that the destination file does not exists, otherwise the query will fail, this is done to avoid overwriting files with random content. The alternative, if you want to append results, for example, is to use mysqldump or something like this:
mysql -uVAR -pVAR -e "SELECT 'hello';" > /tmp/random.log
To execute, if using PHP, from exec().
But looking at previous answers, now I'm not sure is this you where searching for.