2013-03-05 60 views
0

我有以下幾點:爲什麼我的CSV輸出包含每個列兩次?

$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列和一個surname列。生成的csv文件每次顯示兩列。爲什麼?

回答

6

變化mysql_fetch_array()變爲mysql_fetch_assoc()mysql_fetch_array()提取數據庫結果的數字和關聯數組。

while($result = mysql_fetch_assoc$results)) 
{ 
    fputcsv($outstream, $result); 
} 
相關問題