Okay, here's one way you could do it:
Let's say you want the first name, last name, and age from the database with age > 10. Your SELECT could be:
SELECT fname, lname, age FROM People WHERE age > 10;
If you wanted to allow the user to download the results in tab delimited format, try:
SELECT concat_ws( '\t', fname, lname, age ) AS output FROM People WHERE age > 10;
This will give you the comma delimited output of the query as a single field called "output". Put this on a seperate script/page, something like this:
<?php
print header( "Content-type: application/data" );
include( 'db.inc' );
$sql = "SELECT concat_ws( '\t', fname, lname, age ) AS output FROM People WHERE age > 10";
$result = mysql_query( $sql );
while( $row = mysql_fetch_array( $result )) {
print $row[output] . "\n";
}
?>
You could pass the original $sql variable in a session or a cookie and use regexp functions to change the statement without the concat_ws() function to include it (using SELECT and FROM as the delimiters for the regexp).
The header() is what tells the browser to download the file instead of display it. If you changed this to be "Content-type: text/plain", you could view the output in the browser and then the client could use File->Save As to save the file.
Note that the browser will default to naming the file the same as the name of the file generating the output. So, if you named the file 'download.php4', then the browser will default to using 'download.php4' as the filename.
Hope this helps!
-Rich