The server will only send a file to the browser in response to a request from the browser, so the browser has to initiate the process in some way. One suggestion would be some Javascript that requests a certain page on the occasion of some event (page load, mouse click, or whatever works for you).
On this page, as well as all the database-specific stuff, will be the lines
header("Content-type: application/octet-stream");
$attachment=(!strstr($HTTP_USER_AGENT,"Mozilla/4") ||
strstr($HTTP_USER_AGENT,"MSIE"))?' attachment;':'';
header("Content-Disposition:$attachment filename=$filename");
echo $text;
Which will cause the browser to bring up a download prompt to save a file called "$filename", which will contain the contents of the $text variable.
Hope this starts you off.