将查询导出为CSV

时间:2010-08-11 15:53:27

标签: php

我需要将此查询的结果导出到.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);

2 个答案:

答案 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/