开发者

Exporting a query to CSV

i need to export the results of this query into a .csv so i can create a chart i just haven't any idea how to go about it 开发者_JAVA技巧and im still semi new to php thanks for any help.

    $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);


Try iterating thru the result set and use fputcsv to write the rows to a file. http://php.net/manual/en/function.fputcsv.php

For example:

//continuing from your code above:
$fp = fopen('file.csv', 'w');

while ($row = mysql_fetch_assoc($result)) {
    fputcsv($fp,$row);
}

fclose($fp);


it is very strait-forward though...

check this: http://snipplr.com/view/2234/export-mysql-query-results-to-csv/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜