I’m trying to export mysql data to an excel spreadsheet. I’ve looked around at some different code and found code that other people have been able to use, but I can’t seem to get it to work. Here is the code that I’m using:
<?php require_once('Connections/connPavlishDB.php'); ?>
<?php
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=customers.xls");
header("Pragma: no-cache");
header("Expires: 0");
mysql_select_db($database_connPavlishDB, $connPavlishDB) or die(mysql_error());
$select = "SELECT * FROM customers";
$export = mysql_query($select);
$count = mysql_num_fields($export);
for ($i = 0; $i < $count; $i++) {
$header .= mysql_field_name($export, $i)." ";
}
while($row = mysql_fetch_row($export)) {
$line = '';
foreach($row as $value) {
if ((!isset($value)) OR ($value == "")) {
$value = " ";
} else {
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . " ";
}
$line .= $value;
}
$data .= trim($line)."
";
}
$data = str_replace("\r", "", $data);
if ($data == "") {
$data = "
(0) Records Found!
";
}
print "$header
$data";
?>