Exporting data from database to Excel / CSV file is not an easy task to do especially for beginners who just start development in php. This tut for those who want’s to create Excel / CSV file online from database. I am using PHP to do this task and write small but effective code to learn this thing.

Step 1: SQL 

First we create a database where we insert our data.

Step 2. Create connection to Database

Now we create a file { conn.php } where we can create connection to our database.

Step 3. Upload .csv file to server and read data from CSV file

First we need know what is CSV before we are going to do further.


A comma-separated values (CSV) (also sometimes called character-separated values, because the separator character does not have to be a comma) file stores tabular data (numbers and text) in plain-text form. Plain text means that the file is a sequence of characters, with no data that has to be interpreted instead, as binary numbers. { wiki }.

We need to upload .csv file to server before we read data from file. I assume that you already know how we can upload any file to server using form. After uploading file to server we open that file using PHP fopen() function and read data and insert data to database from file as display below in code.

Step 4. Export data from MySQL to CSV file 

Now we can export data from database to .CSV file and trigger download of that file in browser using php header() function as shown in code below.

Step 5. Export data from MySQL to Excel file

In this step we export or database data to Excel.

This is a simple and easy tutorial to create excel or csv file using MySQL database store data.


Posted in: php