title: Export a mysql table into CSV using PHP code
body: <p>I have a my sql table called pvdata, I would like to export it to csv file.</p>
  
  <p>But I'm obtaining the following results instead of the normal looking table:</p>
  
  <pre><code> &lt;br /&gt;
   &lt;font size='1'&gt;&lt;table class='xdebug-error xe-notice' dir='ltr' border='1'cellspacing='0' cellpadding='1'&gt;
   &lt;tr&gt;&lt;th align='left' bgcolor='#f57900' colspan="5"&gt;&lt;span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'&gt;( ! )&lt;/span&gt; Notice: Undefined variable: header in C:\wamp\www\EXPORT TABLE\index.php on line &lt;i&gt;28&lt;/i&gt;&lt;/th&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;th align='left' bgcolor='#e9b96e' colspan='5'&gt;Call Stack&lt;/th&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;th align='center' bgcolor='#eeeeec'&gt;#&lt;/th&gt;&lt;th align='left' bgcolor='#eeeeec'&gt;Time&lt;/th&gt;&lt;th align='left' bgcolor='#eeeeec'&gt;Memory&lt;/th&gt;&lt;th align='left' bgcolor='#eeeeec'&gt;Function&lt;/th&gt;&lt;th align='left' bgcolor='#eeeeec'&gt;Location&lt;/th&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td bgcolor='#eeeeec' align='center'&gt;1&lt;/td&gt;&lt;td bgcolor='#eeeeec' align='center'&gt;0.0035&lt;/td&gt;&lt;td bgcolor='#eeeeec' align='right'&gt;256088&lt;/td&gt;&lt;td bgcolor='#eeeeec'&gt;{main}(  )&lt;/td&gt;&lt;td title='C:\wamp\www\EXPORT TABLE\index.php' bgcolor='#eeeeec'&gt;..\index.php&lt;b&gt;:&lt;/b&gt;0&lt;/td&gt;&lt;/tr&gt;
  &lt;/table&gt;&lt;/font&gt;
  "id id  state   id  state   Longitude   id  state   Longitude   Latitude    id  state   Longitude   Latitude    Altitude(km)    id  state   Longitude   Latitude    Altitude(km)    Module Tilt id  state   Longitude   Latitude    Altitude(km)    Module Tilt Module Azimuth  id  state   Longitude   Latitude    Altitude(km)    Module Tilt Module Azimuth  Rated Peak Power(kW)    id  state   Longitude   Latitude    Altitude(km)    Module Tilt Module Azimuth  Rated Peak Power(kW)    Temperature losses coefficient  id  state   Longitude   Latitude    Altitude(km)    Module Tilt Module Azimuth  Rated Peak Power(kW)    Temperature losses coefficient  Nominal Operation Cell Temperature  id  state   Longitude   Latitude    Altitude(km)    Module Tilt Module Azimuth  Rated Peak Power(kW)    Temperature losses coefficient  Nominal Operation Cell Temperature  Invertor Effeciency id  state   Longitude   Latitude    Altitude(km)    Module Tilt Module Azimuth  Rated Peak Power(kW)    Temperature losses coefficient  Nominal Operation Cell Temperature  Invertor Effeciency Persil Name &lt;br /&gt;"
  &lt;font size='1'&gt;&lt;table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'&gt;
  &lt;tr&gt;&lt;th align='left' bgcolor='#f57900' colspan="5"&gt;&lt;span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'&gt;( ! )&lt;/span&gt; Notice: Undefined variable: data in C:\wamp\www\EXPORT TABLE\index.php on line &lt;i&gt;49&lt;/i&gt;&lt;/th&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;th align='left' bgcolor='#e9b96e' colspan='5'&gt;Call Stack&lt;/th&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;th align='center' bgcolor='#eeeeec'&gt;#&lt;/th&gt;&lt;th align='left' bgcolor='#eeeeec'&gt;Time&lt;/th&gt;&lt;th align='left' bgcolor='#eeeeec'&gt;Memory&lt;/th&gt;&lt;th align='left' bgcolor='#eeeeec'&gt;Function&lt;/th&gt;&lt;th align='left' bgcolor='#eeeeec'&gt;Location&lt;/th&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td bgcolor='#eeeeec' align='center'&gt;1&lt;/td&gt;&lt;td bgcolor='#eeeeec' align='center'&gt;0.0035&lt;/td&gt;&lt;td bgcolor='#eeeeec' align='right'&gt;256088&lt;/td&gt;&lt;td bgcolor='#eeeeec'&gt;{main}(  )&lt;/td&gt;&lt;td title='C:\wamp\www\EXPORT TABLE\index.php' bgcolor='#eeeeec'&gt;..\index.php&lt;b&gt;:&lt;/b&gt;0&lt;/td&gt;&lt;/tr&gt;
  &lt;/table&gt;&lt;/font&gt;"id  state   Longitude   Latitude    Altitude(km)    Module Tilt Module Azimuth  Rated Peak Power(kW)    Temperature losses coefficient  Nominal Operation Cell Temperature  Invertor Effeciency Persil Name "
  "1  ""persil""  ""0""   ""0""   ""0""   ""0""   ""0""   ""0""   ""0.0047""  ""47""  ""0.9"" ""PERSIL07"""
  "2  ""other""   ""12""  ""12""  ""0""   ""15""  ""150"" ""12""  ""0.0046""  ""45""  ""0.95""    ""predefined"""
  </code></pre>
  
  <p>My php code is:</p>
  
  <pre><code>  &lt;?php
  
  $username = "root";
  $password = "";
  $hostname = "localhost"; 
  
  $dbhandle = mysql_connect($hostname, $username, $password) 
    or die("Unable to connect to MySQL");
  
  $selected = mysql_select_db("user data smart grid",$dbhandle) 
    or die("Could not select Data Base");
  
      header("Content-type: application/csv");
      header("Content-Disposition: attachment; filename=pvdata.csv");
      header("Pragma: no-cache");
      header("Expires: 0");
  
  
  
  $query = "SELECT * FROM pvdata";
  
  $export = mysql_query ($query ) or die ( "Sql error : " . mysql_error( ) );
  
  $fields = mysql_num_fields ( $export );
  
  for ( $i = 0; $i &lt; $fields; $i++ )
  {
      $header .= mysql_field_name( $export , $i ) . "\t";
  
      echo $header;
  }
  
  while( $row = mysql_fetch_row( $export ) )
  {
      $line = '';
      foreach( $row as $value )
      {                                            
          if ( ( !isset( $value ) ) || ( $value == "" ) )
          {
              $value = "\t";
          }
          else
          {
              $value = str_replace( '"' , '""' , $value );
              $value = '"' . $value . '"' . "\t";
          }
          $line .= $value;
      }
      $data .= trim( $line ) . "\n";
  }
  $data = str_replace( "\r" , "" , $data );
  
  if ( $data == "" )
  {
      $data = "\n(0) Records Found!\n";                        
  }
  
  print "$header\n$data";
  
  exit();
  
  ?&gt;
  </code></pre>
  
  <p>so what's going wrong? 
  If I export the table manually from the phpmyadmin, it works fine.
  I also want to get the names of the columns in the table if possible.</p>