我有以下内容:
$name = 'registrations.csv';
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename='. $name);
header('Pragma: no-cache');
header("Expires: 0");
$outstream = fopen("php://output", "w");
$results = mysql_query("select * from registrations");
while( $result = mysql_fetch_array($results) )
{
fputcsv($outstream, $result);
}
fclose($outstream);
Run Code Online (Sandbox Code Playgroud)
这很好用,除了它给出了每列中的两列.例如,我在表中有一个given_name列和一个surname列.生成的csv文件显示每列两次.为什么?
更改mysql_fetch_array()到mysql_fetch_assoc().mysql_fetch_array()获取数据库结果的数字和关联数组.
while( $result = mysql_fetch_assoc$results) )
{
fputcsv($outstream, $result);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
197 次 |
| 最近记录: |