Forum Moderators: coopster
$conn = db_connect();
$select = "SELECT * FROM link";
$export = mysql_query($select);
$fields = mysql_num_fields($export);
// loop and extract all field names from db
for ($i = 0; $i < $fields; $i++) { // ### OK checked get field names # echo mysql_field_name($export, $i);
$header .= mysql_field_name($export, $i) . "t";
}
// loop and export the values from the db and write them into the XL columns
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);
// default message
if ($data == "") {
$data = "n(0) Records Found!n";
}
// Auto D-load
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=extraction.xls");
header("Pragma: no-cache");
header("Expires: 0");
// as is in XL first line lists all fields without separation
//and the bulk of data in one line, without data separations
print "$header\n$data";
?>
Generating a CSV for Download from MySQL [webmasterworld.com] msg9