$result = mysql_query("SELECT * FROM table ORDER BY column DESC");
// I hard-code the column names so I can capitalize, add spaces, etc.
$fields = '"User ID","Name","Email","Registration Date"'."\n";
// Iterate through the rows of data
while ($row = mysql_fetch_assoc($result))
{
$fields .= '"'.$row['id'].'","'.$row['name'].'","'.$row['email'].'","'.$row['registration_date'].'"'."\n";
}
// Set our headers
header('Content-type: text/csv');
// To display data in-browser, change the header below to:
// header("Content-Disposition: inline");
header("Content-Disposition: attachment; filename=event_registrations.csv");
// Output our data
echo $fields;