Do you want to run a database query on a remote server and save the results into a text file on your LOCAL disc - i.e. the disc of the computer at which you're seated and not the disc of the server that's in a remote location?
It's not as easy as you think it should be. Because of the security risks involved, you cannot save to LOCAL OUTFILE from a SELECT in MySQL, even though you can read from LOCAL INFILE in a LOAD. Reading in known data is OK, but saving what could be malicious or confidential information away from the server is quite a different kettle of fish.
Solution?
One possibility is to wrap your query in a web page, and allow your browser's facilities to store the result set locally.
In other words, to put up a form and have the user enter his query in that. When submitted, the user's browser deals with the security issues of saving the data locally using the security preferences already set up by the user for his browser..
I've written a sample script to demonstrate the saving of a query result to a local .csv file. You could change the comma to a tab separator for your own use and that way you'll get a page that save a databases to your local system suitable for import to spreadsheets such as Excel.
Run the script
here
View the source
here
Other common questions on MySQL answered
here
UPDATE - Summer '07. See also
additional comments about the header() function and changes needed to the original script for new PHP versions!. Thank you.
(written 2006-01-29 06:12:41)
Associated topics are indexed under
H113 - Using MySQL Databases in PHP PagesS157 - More MySQL Commands
Some other Articles
Looking for Python staffLoosing breath with GeraldRemember to process blank linesDWIM and AWWOSaving a MySQL query results to your local disc for ExcelWhat to do with milkShort Linux and Perl courses for small groupsWoman screaming at meLearning to program in Perl or PHPWhy run two different web servers