in your actual code, you would replace the "output 1 line of data" part with a recordset loop to output all your records (which means you would have to run the same query).
In your main page where you display the records in html, you can add a submit button for export to CSV. When you submit the form to create CSV, it should open a dialogue for "save as", and then you can save the csv file in your local machine.
have not tried FtB's way before. I might try it the next time i work on csv export
Sorry for posting again (slow day)... I found a DW extension that will download to CSV. I haven't used it, but this site is usually reliable for this sort of thing.