$result= mysql_query(“sql query”);

if(mysql_num_rows($result)> 0)
{
$num_fields=mysql_num_fields($result);
$headers = array(“Col1 Name”,”Col2 Name”,”Col3 Name”);
$fp = fopen(‘php://output’, ‘w’);
if ($fp && $result) {
header(‘Content-Type: text/csv’);
header(‘Content-Disposition: attachment; filename=”export.csv”‘);
header(‘Pragma: no-cache’);
header(‘Expires: 0’);
fputcsv($fp, $headers);
while ($row=mysql_fetch_assoc($result)) {
fputcsv($fp, array_values($row));
}
die;
}
}
else
{
echo “No Data Found”;
}