我需要将此查询的结果导出到.csv,这样我就可以创建一个图表,我只是不知道如何去做它,我还是半新的php感谢任何帮助。
$query="SELECT familyID, Fam_End_Date, Fam_Start_Date,
DATEDIFF(date(Fam_End_Date), date(Fam_Start_Date))
AS Days_Between,
TIMEDIFF(time(Fam_Start_Date), time(Fam_End_Date))
AS Time_Between
FROM family
WHERE Fam_End_Date IS NOT NULL
AND Fam_Start_Date IS NOT NULL
AND year(Fam_Start_Date)='$year'";
$result = mysql_db_query($aidDB, $query, $connection);
答案 0 :(得分:2)
尝试通过结果集进行迭代,并使用fputcsv将行写入文件。 http://php.net/manual/en/function.fputcsv.php
例如:
//continuing from your code above:
$fp = fopen('file.csv', 'w');
while ($row = mysql_fetch_assoc($result)) {
fputcsv($fp,$row);
}
fclose($fp);
答案 1 :(得分:0)
检查一下: http://snipplr.com/view/2234/export-mysql-query-results-to-csv/