I am trying to create a excel file from Mysql using php. I found this link:
http://forums.digitalpoint.com/showthread.php?t=60681
and used his code. However, it generated an error as following:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: header
Filename: controllers/export_database.php
Line Number: 26
My code in controller:
public function export ()
{
$line1="ID\tProduct\tColor\tSales\t";
$line2="1\tPrinter\tGrey\t13\t";
$line3="2\tCD\tBlue\t15\t";
$line4="3\tDVD\tRed\t7\t";
$line5="4\tMonitor\tGreen\t4\t";
$line6="5\tTelephone\tBlack\t2\t";
$data="$line1\n$line2\n$line3\n$line4\n$line5\n$line6\n";
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=extraction.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data"; //line 26
}
}
I am not sure why header is an undefined variable. Any thoughts? Thanks for help.
The
header()function pushes that data to the browser for you. It doesn’t create a $header variable, hence the error.Remove
$header.