I have followed a tutorial on phpFreaks http://www.phpfreaks.com/tutorials/114/0.php to export mysql table data to ms excel. It prints the data to the screen but doesn't prompt for download.
Here's my code:
<?php
$user="user";
$host="localhost";
$password="pass";
$database = "db";
$connection = mysql_connect($host,$user,$password)
or die ("couldn't connect to server");
if ($db = mysql_select_db($database,$connection))
echo"connection successful";
if (check_valid_user_admin())
{
$select = "SELECT * FROM tbl_applicants";
$export = mysql_query($select);
$fields = mysql_num_fields($export);
for ($i = 0; $i < $fields; $i++) {
$header .= mysql_field_name($export, $i) . "\t"; }
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);
if ($data == "") {
$data = "\n(0) Records Found!\n";
}
$suffix = date(dmy);
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=cap_roster_export.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
}
?>
All the data looks like it's formatted well but just shows up on the page and never prompts for download.
This question was already asked by someone, but nobody answered it...
Please help me!
Thanks in advance,
Olivier