Set the file name in "SELECT ... INTO DUMPFILE"
Because the fileName value is from the database you don't need to put the single quotes around it.

SELECT data FROM table WHERE id = 123 
INTO DUMPFILE fileName


I'm not sure how filenames with spaces will come out though if you have any. You could always protect against that by using REPLACE on the filename to either remove them or swap them out with a different character before hand.