title: Generate an excel file from MYSQL with PHPExcel
body: <p>I am using PHPExcel to generate an excel file after the end user clicks a "generate excel file" button. This button should open up an excel file for the client, and display the data. So far I have this code, but currently it only opens a blank page with no excel file. can anyone tell me what I am doing wrong? I am guessing that I'm not connecting to the database correctly..</p>
  
  <pre><code>&lt;?php 
  $dbhost= "mysql"; //your MySQL Server 
  $dbuser = "survey"; //your MySQL User Name 
  $dbpass = "password"; //your MySQL Password 
  $dbname = "database"; 
  //your MySQL Database Name of which database to use this 
  $tablename = "questions"; //your MySQL Table Name which one you have to create excel   file 
  // your mysql query here , we can edit this for your requirement 
  $sql = "Select * from $table "; 
  //create  code for connecting to mysql 
  $Connect = @mysql_connect($dbhost, $dbuser, $dbpass) 
   or die("Couldn't connect to MySQL:&lt;br&gt;" . mysql_error() . "&lt;br&gt;" . mysql_errno()); 
   //select database 
   $Db = @mysql_select_db($dbname, $Connect) 
  or die("Couldn't select database:&lt;br&gt;" . mysql_error(). "&lt;br&gt;" . mysql_errno()); 
  //execute query 
  $result = @mysql_query($sql,$Connect) 
  or die("Couldn't execute query:&lt;br&gt;" . mysql_error(). "&lt;br&gt;" . mysql_errno()); 
  
   error_reporting(E_ALL);
  
   require_once '../Classes/PHPExcel.php';
   $objPHPExcel = new PHPExcel();
  
   // Set the active Excel worksheet to sheet 0 
  
  $objPHPExcel-&gt;setActiveSheetIndex(0);  
  
  // Initialise the Excel row number 
  
  $rowCount = 1;  
  
  // Redirect output to a client’s web browser (Excel5) 
  header('Content-Type: application/vnd.ms-excel'); 
  header('Content-Disposition: attachment;filename="results.xls"'); 
  header('Cache-Control: max-age=0'); 
  $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); 
  $objWriter-&gt;save('php://output');
  
  exit;
  </code></pre>