title: MSSQL Select export to CSV
body: <p>I try to create a csv file with selected rows from a mssql database.
  The export works but the formatting is wrong.</p>
  
  <p>PHP:</p>
  
  <pre><code>require_once("config/config.php");
  
  $connectionInfo = array("Database"=&gt;DB_DB, "UID"=&gt;DB_USER, "PWD"=&gt;DB_PASSWORD);
  $conn = sqlsrv_connect(DB_HOST, $connectionInfo);
  
  if ($conn === false ) {
      die (print_r(sqlsrv_errors(), true));
  }
  
  $sql = "SELECT * FROM [RC.Appointments]";
  $result = sqlsrv_query($conn, $sql);
  
  if (!$result) die ('Couldn\'t fetch records');
  
  $headers = array();
  
  foreach (sqlsrv_field_metadata($result) as $fieldMetadata) {
      $headers[] = $fieldMetadata['Name'];
  }
  
  $fp = fopen('php://output', 'w');
  if ($fp &amp;&amp; $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 = sqlsrv_fetch_array($result, SQLSRV_FETCH_NUMERIC)) {
          fputcsv($fp, array_values($row));
      }
      die;
  }
  </code></pre>
  
  <p>Dont know why there are no headers and I need the data seperate by columns.
  Is the problem caused by the missing headers?</p>
  
  <p>the output in sublimetext</p>
  
  <pre><code>appointment_id,terminname,datum
  151,"Bitte Terminnamen vergeben",18.02.2014
  152,"Bitte Terminnamen vergeben",19.02.2014
  153,"Bitte Terminnamen vergeben",20.02.2014
  154,"Bitte Terminnamen vergeben",25.02.2014
  155,"Bitte Terminnamen vergeben",26.02.2014
  156,"Bitte Terminnamen vergeben",27.02.2014
  157,"Bitte Terminnamen vergeben",31.12.2014
  </code></pre>