我有以下内容:
$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);
这个工作得很好,除了它每列给出两个。例如,我在表中有一个given_name列和一个姓氏列。生成的csv文件将每列显示两次。为什么?
将mysql_fetch_array()
改为mysql_fetch_assoc()
。mysql_fetch_array()
获取数据库结果的数字数组和关联数组。
while( $result = mysql_fetch_assoc$results) )
{
fputcsv($outstream, $result);
}