Saving a MySQL query results to your local disc for Excel
Archive - Originally posted on "The Horse's Mouth" - 2006-01-29 06:12:41 - Graham Ellis
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