Pretty simple, first, put the following commands before anything in your script:
Header("Content-disposition: attachment; filename=\"data.txt\"");
Header("Content-type: application/download");
This will tell the browser to use the filename "data.txt" (or whatever you prefer) then the Content-type is not one recognized by the browser, so it will save it to disk. After that, just echo the information from your database to the browser. Simple as that.
Hope that helps:
Chris King