title: How to export an SQL table to an excel format with PHP
body: <p>So I need my SQL table to be downloadable. Right now my php file creates an excel file and forces a download but the excel file just has a large array with the table's data inside it.</p>
  
  <p>Here is my code</p>
  
  <pre><code>$sql = mysql_query("SELECT * FROM table");
  
  while ($row_user = mysql_fetch_assoc($sql))
  {   $userinfo[] = $row_user;
  
  print_r($row_user);
  
  header("Content-Disposition: attachment; filename=\"papi.xls\"");
  header("Content-Type: application/vnd.ms-excel;");
  header("Pragma: no-cache");
  header("Expires: 0");
  $file = fopen("php://output","w");
  
  foreach($userinfo as $data)
     {
    fputcsv($file,explode(',',$data),"\t");
   }
  
    fclose($file);
   }
  </code></pre>
  
  <p>Any help would be greatly appreciated, thank you in advance.</p>