title: fputcsv with custom headers
body: <p>Trying to convert the following working code to PDO, the problem I am running into are the custom headings set in the <code>fputcsv($output,array)</code>. I really want to move away from all msql_ functions so I would greatly appreciate some help.</p>
  
  <pre><code>&lt;?php
  // output headers so that the file is downloaded rather than displayed
  header('Content-Type: text/csv; charset=utf-8');
  header('Content-Disposition: attachment; filename=collections.csv');
  
  // create a file pointer connected to the output stream
  $output = fopen('php://output', 'w');
  
  
  //MySQL Connect
  removed
  
  // output the column headings
  fputcsv($output, array('Title','InclusiveDates','CollectionID','LocationID','Content','RangeValue','Section','Shelf','Extent','ExtentUnitID'));
  
  // fetch the data
  
  $rows = mysql_query('SELECT 
  coll.title AS CollectionTitle, 
  coll.inclusivedates AS InclusiveDates,
  coll.ID AS CollectionID, 
  collloc.LocationID,
  collloc.Content,
  collloc.RangeValue,
  collloc.Section,
  collloc.Shelf,
  collloc.Extent,
  collloc.ExtentUnitID
  FROM tblCollections_Collections coll 
  JOIN tblCollections_CollectionLocationIndex collloc 
  ON collloc.CollectionID = coll.id 
  ORDER BY coll.ID');
  
  // loop over the rows, outputting them
  while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row);
  
  ?&gt;
  </code></pre>
  
  <p>I have gotten this far, without getting it to work. The problem here is that I am not able to set custom column names in the CSV file and it also does not export to a CSV.</p>
  
  <pre><code>&lt;?php
  $conn = removed
  
  
  $query = "SELECT 
  coll.title AS CollectionTitle, 
  coll.inclusivedates AS InclusiveDates,
  coll.ID AS CollectionID, 
  collloc.LocationID,
  collloc.Content,
  collloc.RangeValue,
  collloc.Section,
  collloc.Shelf,
  collloc.Extent,
  collloc.ExtentUnitID
  FROM tblCollections_Collections coll 
  JOIN tblCollections_CollectionLocationIndex collloc 
  ON collloc.CollectionID = coll.id 
  ORDER BY coll.ID";
  $stmt = $conn-&gt;prepare($query);
  
  // Execute the statement
  $stmt-&gt;execute();
  
  var_dump($stmt-&gt;fetch(PDO::FETCH_ASSOC));
  
  $data = fopen('file.csv', 'w');
  
  $header = array();
  
  while ($row = $stmt-&gt;fetch(PDO::FETCH_ASSOC))
  {
  if(empty($header)){ // do it only once!
    $header = array_keys($row); // get the columnnames
    fputcsv($data, $header); // put them in csv
  }
  
  echo "Success";
  // Export every row to a file
  fputcsv($data, $row);
  }
  ?&gt;
  </code></pre>