PHP > CSV help

Permabanned
Joined
22 Apr 2007
Posts
1,805
Hi guys,

I have a single MySQL database with around 8 fields in it.

I want to export two of these fields into a CSV file so that it can be read by chart plotting software.

I have no real idea how to do this and could do with some pointers from some kind souls.
 
Code:
header('Content-type: text/csv');
$result = some mysql stuff // assume that $result now contains an array of your result set
foreach ($result as $re) {
  foreach ($re as $r) {
    $r = str_replace('"','\"',$r); // so that any double quotes don't escape your field in the CSV
    printf('"%s",',$r);
  }
  echo "\n";
}

that should give you a csv
 
Back
Top Bottom