Wyeksportuj tabelę mysql do CSV za pomocą kodu PHP

Mam tabelę sql o nazwie pvdata, chciałbym wyeksportować ją do pliku csv.

Ale otrzymuję następujące wyniki zamiast normalnie wyglądającej tabeli:

 <br />
 <font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1'cellspacing='0' cellpadding='1'>
 <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined variable: header in C:\wamp\www\EXPORT TABLE\index.php on line <i>28</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0035</td><td bgcolor='#eeeeec' align='right'>256088</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\EXPORT TABLE\index.php' bgcolor='#eeeeec'>..\index.php<b>:</b>0</td></tr>
</table></font>
"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 <br />"
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined variable: data in C:\wamp\www\EXPORT TABLE\index.php on line <i>49</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0035</td><td bgcolor='#eeeeec' align='right'>256088</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp\www\EXPORT TABLE\index.php' bgcolor='#eeeeec'>..\index.php<b>:</b>0</td></tr>
</table></font>"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"""

Mój kod php to:

  <?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 < $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();

?>

więc co się dzieje źle? Jeśli ręcznie wyeksportuję tabelę z phpmyadmin, działa dobrze. Chcę też, jeśli to możliwe, uzyskać nazwy kolumn w tabeli.

questionAnswers(2)

yourAnswerToTheQuestion