Export To Excel with Headers In PHP and MySQL | How to Export MySQL Data in Excel/CSV File using PHP/MySQL

export to excel from mysql in php

Export MySql data to Excel example

So many time we need to export data from MySql to Excel or CSV. So many time we need to send reports in excel and we require to export data from MySql to Excel.

We can perform this task very easily in PHP. Here in this article I'm going to explain how we can export the MySql data into excel or CSV in PHP.

I will be writing single PHP script which will work for excel and CSV both. We can do it by just changing the headers of file.

Here I'm creating the users table in 'csv' MySql database. The 'users' table contains the data of users and we need to export this users data.

Here is MySql queries you just need to execute these queries in you database panel like phpmyadmin.

Here is the database connection file. Here we are creating MySql database connection with PHP.

The below code is used to list all the users.

Below code is used to export the MySql data to Excel or CSV files.

Here if you are exporting the Excel file then we need to using following code:

And if you wanted to export file of 'CSV' type then use following code:


Share This:

Leave a Reply

Your email address will not be published. Required fields are marked *