Extract from MySQL to XLS (code included)
Posted: Fri Oct 07, 2005 7:42 am
good moring phpdn!
i have a question this morning about extracting from a MySQL database, and exporting it to an .xls file.
i have the database all set up, and the form is putting all the info in the right fields and such and such...
but with my code, there is a no go.
i have never even worked with excel, let alone tried to extract data for it... i was wondering if anyone had any experience with it, or at the very least, how to extract to a text file... as a last resort.
the code below is in xls.inc.php, and is below the INSERT statement, nested in the if('bpress'='add') tag.
let's see if it's in the include, or if it is probably the way i have it on the page.
here's what i got:
Good Morning!
i have a question this morning about extracting from a MySQL database, and exporting it to an .xls file.
i have the database all set up, and the form is putting all the info in the right fields and such and such...
but with my code, there is a no go.
i have never even worked with excel, let alone tried to extract data for it... i was wondering if anyone had any experience with it, or at the very least, how to extract to a text file... as a last resort.
the code below is in xls.inc.php, and is below the INSERT statement, nested in the if('bpress'='add') tag.
let's see if it's in the include, or if it is probably the way i have it on the page.
here's what i got:
Code: Select all
<? $select = "SELECT * FROM surveys WHERE Mem_ID =".$_COOKIE["ID02"];
$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";
}
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=extract.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
?>Good Morning!