AdBlock Detected

We provide high-quality source code for free. Please consider disabling your AdBlocker to support our work.

Buy me a Coffee

Saved Tutorials

No saved posts yet.

Press Enter to see all results

Export data to CSV from MySQL

By pushpam abhishek
Listen to this article

Export data to CSV from MySQL


Many times I get requests from the client for the data dump. I use MySQL database, and in MySQL, it is quite
easy to create CSV files directly from MySQL with just one query!

Let's say you want to export the email and name fields from your member table to a CSV file. Here is your code:

SELECT email, name INTO OUTFILE '/tmp/data_dump_member.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM member

Note: Make sure your MySQL server has to write permissions to the location where you want to store the results file.

You can either download the CSV file from the server's '/tmp' directory, or you can move this to the HTTP root directory and send a link to the client to download.
cd /tmp/

mv data_dump_member.csv /var/www/html/
gzip -9 data_dump_member.csv


Share this post

pushpam abhishek

About pushpam abhishek

Pushpam Abhishek is a Software & web developer and designer who specializes in back-end as well as front-end development. If you'd like to connect with him, follow him on Twitter as @pushpambhshk

Comments