Basically, you'll probably want to create a separate PHP script for each file to be downloaded. You'll need to communicate to that script the info needed to make the necessary query. This could be done via $_SESSION variables if you are using sessions, or else adding name=value pairs in the URL query string of the link to that file.
Then when the user clicks the link to the download script file, the script will take those $SESSION or $GET values and perform the query, then echo out the data in the desired format. If desired, you can force the file to be downloaded (rather than possibly displaying in the browser) with the following headers in the download script before any other output is generated:
header('Content-Type: text/ini');
header('Content-Disposition: attachment; filename="myapp.ini"');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');