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:
Table of Contents
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 user’s 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)
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(40) NOT NULL,
`last_name` varchar(40) NOT NULL,
`email` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `users` (`id`, `first_name`, `last_name`, `email`) VALUES
(1, 'Jerry', 'McKenny', 'jerrymckenny@gmail.com'),
(2, 'Lillian', 'Evans', 'lillianevans@gmail.com'),
(3, 'Eva', 'Osborne', 'evaosborne@gmail.com'),
(4, 'Darlene', 'Edwards', 'darleneedwards@gmail.com'),
(5, 'Bill', 'White', 'billwhite@gmail.com');
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 gets downloaded
If you check download file, you get records listed, as showing below:
We are done, you can download the complete source for reference use following links to download or checkout live demo. Live Demo
Next you can learn how to import data from CSV to MySQL here – Import Data from CSV File to MySQL Using PHP
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 ?
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!
Great Article, I wonder have you tried the header display in the column rather than row?
when i’m use online then it’s not download the file
just show me
No,”First Name”,”Last Name”,Email,”Phone Num”,Address,College/university,”Seminar date”,”Seminar shift”,Message 5,sommer,sfasdjfh,asdf@gmail.com,12344587098,fhaljkh,lkasjdhf,”Sep 15, 2018″,morning,asdf
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
Great Article,Works all fine with me.
not working and Im not using any paswords
Warning: mysqli::__construct(): (HY000/1045): Access denied for user ‘root’@’localhost’ (using password: YES) in C:xampphtdocsEXPORTconnect.php on line 27
Connection failed: Access denied for user ‘root’@’localhost’ (using password: YES)
It looks like you have an issue connecting to mySQL Database, make sure you specify correct connection details.
Hello, it also export the html code inside the csv file.
How to avoid that ?
thx