Hi
I am using the following lines of code to extract data from a table within my database and save it into an excel spreadsheet, however, each time it runs it seems to save the page I run the code from into the excel document rather than get the rows of data from the database! The best way to describe it would be that it takes a print screen of my webpage and dumps that into the XLS.
Anyway, here is the code:
$select = "SELECT * FROM tblregistrants";
$export = mysql_query($select);
$fields = mysql_num_fields($export);
// get header information for output
for ($i = 0; $i < $fields; $i++) {
$header .= mysql_field_name($export, $i) . "\t";
}
// now get the rows of data from the database
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);
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=extraction.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data"; // output
If someone could be kind enough to maybe point out what on earth is going wrong I would be grateful!!
Thanks