Export database record to Excel in PHP

In most of web portals admin need the report of website in daily or monthly basis to monitor his website. In this article i will explain you how to generate an report from your database and exporting it as a excel file.

Export database record to excel in PHP

You can view demo and download the example file below

Download File

In this example i have created one form and when u submitting a form the report will generate and you can download it. Find below the PHP code.

//Enter the headings of the excel columns
$contents="id,day name,Date,urls,titles,status

//Mysql query to get records from datanbase
//You can customize the query to filter from particular
date and month etc...Which will depends your database structure.
$user_query = mysql_query('select id,display_date,url,
title,status from tablename');

//While loop to fetch the records
while($row = mysql_fetch_array($user_query))

// remove html and php tags etc.
$contents = strip_tags($contents); 

//header to make force download the file
Header("Content-Disposition: attachment; filename=
print $contents;

PHP will fetch the record and export it as an excel file. You can customize the sql query and get your required output.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>