croceldon
croceldon

Reputation: 4595

Why does my CSV output contain each column twice?

I have the following:

$name = 'registrations.csv';
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename='. $name);
header('Pragma: no-cache');
header("Expires: 0");
$outstream = fopen("php://output", "w");
$results = mysql_query("select * from registrations");
while( $result = mysql_fetch_array($results) )
{
  fputcsv($outstream, $result);
}
fclose($outstream);

This works great, except it gives two of every column. For example, I have a given_name column and a surname column in the table. The resulting csv file shows each column twice. Why?

Upvotes: 0

Views: 324

Answers (1)

John Conde
John Conde

Reputation: 219834

Change mysql_fetch_array() to mysql_fetch_assoc(). mysql_fetch_array() fetches both a numeric and associative array of the database results.

while( $result = mysql_fetch_assoc$results) )
{
  fputcsv($outstream, $result);
}

Upvotes: 6

Related Questions