<?php
// output headers so that the file is downloaded rather than displayed
header(‘Content-Type: text/csv; charset=utf-8’);
header(‘Content-Disposition: attachment; filename=data.csv’);
// create a file pointer connected to the output stream
$output = fopen(‘php://output’, ‘w’);
// output the column headings
fputcsv($output, array(‘ID’,’Name’,’Email’ ));
// fetch the data
mysql_connect(‘localhost’, ‘root’, ‘password’);
mysql_select_db(‘abc’);
$sql=’SELECT name,email FROM users ORDER BY id DESC’;
$rows = mysql_query($sql);
// loop over the rows, outputting them
while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row)