Hello all 🙂
I'm querying a database and trying to put the data into a CSV file. I'm using PHP's fputcsv function to make things simpler. It adds the data to the file no problem, but each column's data is written twice. For example, if my query returns 4 columns, it writes 8 columns (each column twice in succession). I've looked over php.net's documentation on the fputcsv function a couple times now and I cannot see what I am doing wrong.
Here's the code:
if($handle = fopen('endusers.csv', 'w'))
{
$query = "SELECT * FROM endusers NATURAL JOIN signups";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
fputcsv($handle, $row);
echo '<a href="endusers.csv">Export to CSV</a>';
fclose($handle);
}
I've verified that the SQL query is pulling the correct data because I use the exact same query just before this code to display the data, plus I've used it in phpMyAdmin and it retrieves the data correctly (plus the tables are very simple). I've also tried removing the while loop and just adding a single row but the issue occurs then, too.
If someone could shed some light on this (or even provide a simple alternative) I would be greatly appreciative. Thank you in advance! 🙂