Page 1 of 1

Cannot Modify Header Information

PostPosted: 20. January 2007 21:16
by libertyct
just a quick question, i was trying this code, creating an excel file
but i get a "Cannot Modify Header Information" error, what am i doing wrong?

$query = "SELECT fname, lname FROM students";
$result = mysql_query($query) or die('Error, query failed');

$tsv = array();
$html = array();
while($row = mysql_fetch_array($result, MYSQL_NUM))
{
$tsv[] = implode("\t", $row);
$html[] = "<tr><td>" .implode("</td><td>", $row) . "</td></tr>";
}

$tsv = implode("\r\n", $tsv);
$html = "<table>" . implode("\r\n", $html) . "</table>";

$fileName = 'mysql-to-excel.xls';
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$fileName");

echo $tsv;
//echo $html;

PostPosted: 20. January 2007 22:49
by Wiedmann
but i get a "Cannot Modify Header Information" error, what am i doing wrong?

There is an output before the header() function.

PostPosted: 21. January 2007 04:57
by xnemesis
Check where it is in your HTML code. The placement of certain functions is VERY important. Especially when using header() check out php.net for more information