Question

I have a simple process to create a CSV file with data from a database.

if (isset($_POST['submitcsv'])) {
     $csvfile = fopen("stardata.csv", "w"); //w=write, r=read, a=append
     $datebeginning = date('Y/m/d', strtotime($_POST['datebeginning']));
     $query = $pdoace->prepare("SELECT * 
                        FROM `starrisk` 
                        WHERE `testdate` >= ? 
                        AND `site` = '". $_POST['fromlocation'] ."'   
                        ORDER BY `cdcnumber` ASC"); 
        $query->execute(array($datebeginning));
        While($row = $query->fetch()) {
            for ($x=1; $x<=51; $x++)
            {
                   echo fwrite($csvfile, $row[$x].",");//inside loop;
            }
                   echo fwrite($csvfile, "\r\n");
        }
            fclose ($csvfile);
            echo 'Data Compilation Complete: <a href="stardata.csv">download now...</a>';
           }

I get mysterious numbers that appear just the Data Compilation Complete (below) that appear to have nothing to do with the data itself (which consists of a first name, last name, and a bunch of -1 and 1's (a true-false))...

117342222333222233223232233332232232332223323233333332117672223232323222233322222233233232323233323233322222Data Compilation Complete: download now...

Ideas anyone???

Was it helpful?

Solution

The echo you have before fwrite will output the amount of bytes written. Reference: http://php.net/fwrite#refsect1-function.fwrite-returnvalues

OTHER TIPS

Change code to:

 while($row = $query->fetch()) {
    fputcsv ( $csvfile , $row );
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top