tags:

views:

27

answers:

2

hey 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 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);
+2  A: 

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);
Irwin
i added your code and also tried andy's code and got this errorWarning: fopen(file.csv): failed to open stream: Permission denied in on line 19Fatal error: Call to undefined function: fputcsv()0n line 22
Karl Lenz
Karl, does the file you are writing to exist? (that is, do you have a file called "file.csv" already created?
Irwin
A: 

it is very strait-forward though...

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

Andy Lin