Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


PHP MYsql export to Excel Not printing header lables.

Posted on 2009-02-23
Medium Priority
Last Modified: 2013-12-12
For some reason this php script that exports mysql to excel is not printing my header labels.
How would i get it to print the column header labels? Looks like its already setup to do this.
This PHP script Extracts MySQL table and downloads into an Excel Spreadsheet.
Script by Jeff Johns, for a full explanation and tutorial on this, see: http://www.phpfreaks.com/tutorials/114/0.php
YOUR DATABASE HOST = (ex. localhost)
USERNAME = username used to connect to host
PASSWORD = password used to connect to host
DB_NAME = your database name
TABLE_NAME = table in the database used for extraction
To extract specific fields and not the whole table, simply replace
the * in the $select variable with the fields you want
define(db_host, "localhost");
define(db_user, "####");
define(db_pass, "######");
define(db_link, mysql_connect(db_host,db_user,db_pass));
define(db_name, "roster");
Build query, call it, and find the number of fields
$select = "SELECT * FROM employees";
$export = mysql_query($select);
$count = mysql_num_fields($export);
Extract field names and write them to the $header variable
for ($i = 0; $i < $count; $i++) {
$header .= mysql_field_name($export, $i)."\t";
Extract all data, format it, and assign to the $data variable
while($row = mysql_fetch_row($export)) {
$line = '';
foreach($row as $value) {
if ((!isset($value)) OR ($value == "")) {
$value = "\t";
} else {
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . "\t";
$line .= $value;
$data .= trim($line)."\n";
$data = str_replace("\r", "", $data);
Set the default message for zero records
if ($data == "") {
$data = "n(0) Records Found!n";
Set the automatic download section
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=EmployeeRoster.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$headern$data";

Open in new window

Question by:hicommfcu
  • 2
LVL 16

Assisted Solution

Robin earned 800 total points
ID: 23716312
What if you change line #65 to

print $header . $data;
LVL 19

Accepted Solution

Michael701 earned 1200 total points
ID: 23716349
Close, but how about this

print $header. "\n". $data;
LVL 16

Expert Comment

ID: 23716376
Even better.. ;-)

Author Closing Comment

ID: 31550346

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question