Converting MySQL data into csv

I am trying to make data from msql database available to a member of staff as a zip-file, by just clicking a link. The file needs to be converted to csv format before gets downloaded via as a zip-file. With the code below I am getting array to string conversion. Any help please?



    $result = $pdo->query("SELECT * FROM members");

   $file = fopen("$filename", "w");

    $filename = 'images/'.strtotime("now").'.csv';

    $seperator = "";
    $comma = "";

    $data = $result->fetchAll();

    $separator .= $comma . '' . str_replace('','""',$data);
    $commar = ",";

    $separator .= "\n";

    echo $separator;