【发布时间】:2011-05-18 13:20:39
【问题描述】:
所以,首先让我声明,在您开始提供有关如何简单地将完整表导出到 CSV 文件的其他建议之前,这不是我想要做的(我的演示代码现在建议这样做,但我只是通过浏览器 shabaz 测试整个下载)。
我实际上想构建要从多个表中导出的内容。无论如何,我现在只是想通过浏览器进行导出和下载。
我从一个在线示例中获得了这段代码,目前正在发生的事情只是将数据打印到我的浏览器中。
这是我正在测试的代码:
$result = mysql_query("SELECT * FROM results_tb") or die(mysql_error());
// I hard-code the column names so I can capitalize, add spaces, etc.
$fields = '"User ID","Test_id","score %","Time taken"'."\n";
// Iterate through the rows of data
while ($row = mysql_fetch_assoc($result))
{
//echo $row['user_id'];
$fields .= '"'.$row['user_id'].'","'.$row['test_id'].'","'.$row['score'].'","'.$row['time_elapsed'].'"'."\n";
}
// Set our headers
header('Content-type: text/csv');
// To display data in-browser, change the header below to:
// header("Content-Disposition: inline");
header("Content-Disposition: attachment; filename=event_registrations.csv");
// Output our data
echo $fields;
我假设这个例子是裤子,那么有人可以向我解释一下怎么做吗?
非常感谢:D
我的标题响应被 firebug-Lite 捕获:
Date Wed, 18 May 2011 14:15:18 GMT
X-Powered-By PHP/5.1.6
Content-disposition attachment;filename=MyVerySpecial.csv
Connection close
Content-Length 992
Server Apache/2.2.3 (Red Hat)
Content-Type text/csv
【问题讨论】:
-
如果将内容类型更改为 application/octet-stream 有什么变化吗?如果是这样,那么您的浏览器想要处理所有 text/* 类型并忽略处置。
-
嗨,谢谢,不幸的是,我得到了同样的东西。
标签: php mysql file http-headers export-to-csv