我在网上找到了以下脚本并根据需要对其进行了编辑,但我无法弄清楚的一件事是如何更改列名。按原样,列名在电子表格中显示为数据库中相应列的名称。有没有办法改变这个?
感谢您的任何建议!这是我的代码:
<?php
$host = 'asdf';
$user = 'asdf';
$pass = 'asdf';
$db = 'asdf';
$table = 'stats';
$file = 'export';
$fields = "date, name, time, inbset, asapset, faxset, obset, totset, inbclo, asapclo, faxclo, obclo, totclo, inbtot, asaptot, faxtot, obtot, total, comments";
$filename = $file."_".date("Y-m-d");
$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");
$result = mysql_query("SHOW COLUMNS FROM ".$table."");
$i = 0;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row['Field'].", ";
$i++;
}
}
$csv_output .= "\n";
$values = mysql_query("SELECT $fields FROM ".$table."");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j].", ";
}
$csv_output .= "\n";
}
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
?>