我在导出使用php从我的一个mysql表创建的csv文件时遇到了一些麻烦。从Mysql导出CSV
我正在使用的代码会打印正确的数据,但我看不到如何在csv文件中下载此数据,并提供下载链接到创建的文件。我以为浏览器应该自动提供下载文件,但它不会。 (难道是因为下面的代码使用AJAX叫什么名字?)
任何帮助,不胜感激 - 下面的代码,S.
include('../cofig/config.php'); //db connection settings
$query = "SELECT * FROM isregistered";
$export = mysql_query($query) or die("Sql error : " . mysql_error());
$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)) || ($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/octet-stream"); //have tried all of these at sometime
//header("Content-type: text/x-csv");
header("Content-type: text/csv");
//header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=export.csv");
//header("Content-Disposition: attachment; filename=export.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo '<a href="">Download Exported Data</a>'; //want my link to go in here...
print "$header\n$data";
非常感谢大家的帮助 - 非常感谢。 – ss888 2011-01-06 10:49:19