<?php 
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="report.csv"');
header('Pragma: no-cache');    
header('Expires: 0');
     
include("connect.php");
$result = mysqli_query($db,"Select * from doctors"); 
if (!$result) die('Couldn\'t fetch records'); 
$num_fields = mysqli_num_fields($result); 
$headers = array(); 
for ($i = 0; $i < $num_fields; $i++) 
{     
       $headers[] = mysqli_fetch_field_direct($result , $i)->name; 
} 
$fp = fopen('php://output', 'w'); 
if ($fp && $result) 
{     
    
       fputcsv($fp, $headers); 
       while ($row = mysqli_fetch_row($result)) 
       {
          fputcsv($fp, array_values($row)); 
       }
die(); 
} 

?>