Home CSV Export Data from MySQL to CSV Using PHP

Export Data from MySQL to CSV Using PHP


Export MySQL data to CSV

A step-by-step guide to export data to CSV from MySQL using PHP

It is a basic task for any application that needs a reporting feature to CSV; here I am going to explain how to generate a CSV file from the MySQL records.

Let’s get started:

Step 1 – Create Sample Database and Required Table along with the data:

Create Database table with sample records, here I am going to use a users table, which is going to have users details, go ahead and use following SQL statements to create table and insert the records. (Note: you can skip this step if you have your existing database tables)

IMAGE : Users Table Structure
Users Table Structure

Existing records from mysql database
Existing Mysql Records to Export

Step 2 Create PHP to MySQL Database Connection Script:

Create Database configuration file, which is going help us to connect to MySQL from PHP file.

Step 3 – Add Sample Index Page to List out the Records:

Create index.php file and list records.


Step 4- Exporting MySQL Data to CSV file PHP Script:

Create export.php file to export data from Mysql to CSV.


Click OK to export the data to CSV and your file get’s downloaded



If you check download file, you get records listed, as showing below:

Records from CSV files
Records from CSV files

We are done, you can download the complete source for reference use following links to download or checkout live demo.

Learn how to import data from CSV to MySQL  – Import Data from CSV File to MySQL Using PHP

Enjoy this tutorial? Get iTech Empires updates delivered straight to your inbox.

No Spam, ever. We'll never share your email address and you can opt out at any time.

Yogesh Kolihttp://www.itechempires.com
Yogesh Koli is a software engineer & Blogger lives in India. He's driven by an addiction to learning and a love for adventure. he has 6+ years of experience working with the front-end and back-end web application development.


3 Comment threads
1 Thread replies
Most reacted comment
Hottest comment thread
4 Comment authors
SaurabhSteven SmithYogesh KoliArvind Srivastava Recent comment authors

This site uses Akismet to reduce spam. Learn how your comment data is processed.

newest oldest most voted
Notify of

Great one. Works all fine with me. I am just not able to get the file downloaded. The data is displayed properly but download is an issue

Steven Smith
Steven Smith

Great Article, I wonder have you tried the header display in the column rather than row?

Arvind Srivastava
Arvind Srivastava

Thanks for nice tutorial Mysql to csv, I have problem, I don’t want to export all mysql data, I just want to export first Name and email from MYSQL to CSV, How i can do that ?

Yogesh Koli

Simply replace the SQL Query from SELECT * FROM users to SELECT first_name, email from users and update fputcsv($output, array(‘No’, ‘First Name’, ‘Last Name’, ‘Email’)); to fputcsv($output, array(‘No’, ‘First Name’, ‘Email’));

and your done!

Tutorials You May Like!

- Advertisement -