Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP代码将mysql表导出为CSV_Php_Mysql_Csv - Fatal编程技术网

使用PHP代码将mysql表导出为CSV

使用PHP代码将mysql表导出为CSV,php,mysql,csv,Php,Mysql,Csv,我有一个名为pvdata的sql表,我想将其导出到csv文件 但我得到了以下结果,而不是正常的表格: <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">&l

我有一个名为pvdata的sql表,我想将其导出到csv文件

但我得到了以下结果,而不是正常的表格:

 <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"""

(!)注意:未定义变量:第28行C:\wamp\www\EXPORT TABLE\index.php中的标题 调用堆栈 #时间记忆功能定位 10.0035256088{main}()..\index.php:0 “id状态id状态经度id状态经度纬度id状态经度纬度高度(km)id状态经度纬度高度(km)模块倾斜id状态经度纬度高度(km)模块倾斜模块方位id状态经度纬度高度(km)模块倾斜模块方位角额定峰值功率(kW)id状态经纬度高度(km)模块倾斜模块方位角额定峰值功率(kW)温度损耗系数id状态经纬度高度(km)模块倾斜模块方位角额定峰值功率(kW)温度损失系数标称操作单元温度id状态经纬度高度(km)模块倾斜模块方位角额定峰值功率(kW)温度损失系数标称操作单元温度逆变器效率id状态经纬度高度(km)模块倾斜模块方位角额定峰值功率(kW)温度损耗系数标称操作单元温度逆变器效率Persil名称
” (!)注意:未定义变量:第49行C:\wamp\www\EXPORT TABLE\index.php中的数据 调用堆栈 #时间记忆功能定位 10.0035256088{main}()..\index.php:0 “id状态经纬度高度(km)模块倾斜模块方位角额定峰值功率(kW)温度损失系数标称操作单元温度逆变器效率Persil名称” “1”“persil”“0”“0”“0”“0”“0”“0.0047”“47”“0.9”“PERSIL07” “2”“其他”“12”“12”“0”“15”“150”“12”“0.0046”“45”“0.95”“预定义”
我的php代码是:

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

?>

在for循环之前,请将$header初始化为空字符串

$header = '';//initialize header
for ( $i = 0; $i < $fields; $i++ )
{
    $header .= mysql_field_name( $export , $i ) . "\t";

    echo $header;//remove this line
}

您可以使用fputcsv函数清除代码

我不知道这是否是打字错误,但在您的脚本中,在
   <?php
第28行:$header未知,这使得

$header .=

追加到未知变量。

请尝试此代码-

<?php

    // Database Connection

    $host="localhost";
    $uname="root";
    $pass="";
    $database = "a2zwebhelp"; 

    $connection=mysql_connect($host,$uname,$pass); 

    echo mysql_error();

    //or die("Database Connection Failed");
    $selectdb=mysql_select_db($database) or 
    die("Database could not be selected"); 
    $result=mysql_select_db($database)
    or die("database cannot be selected <br>");

    // Fetch Record from Database

    $output = "";
    $table = ""; // Enter Your Table Name 
    $sql = mysql_query("select * from $table");
    $columns_total = mysql_num_fields($sql);

    // Get The Field Name

    for ($i = 0; $i < $columns_total; $i++) {
    $heading = mysql_field_name($sql, $i);
    $output .= '"'.$heading.'",';
    }
    $output .="\n";

    // Get Records from the table

    while ($row = mysql_fetch_array($sql)) {
    for ($i = 0; $i < $columns_total; $i++) {
    $output .='"'.$row["$i"].'",';
    }
    $output .="\n";
    }

    // Download the file

    $filename = time().'csv'; //For unique file name
    header('Content-type: application/csv');
    header('Content-Disposition: attachment; filename='.$filename);

    echo $output;
    exit;

    ?>


礼貌-

因为您的报价单在标题中的签名和数据放置不正确。开始时$header不是空的吗?PHP中的变量在使用前不必进行初始化…您还需要在while循环外部初始化$data。@PAM变量需要初始化,因为它与。=concatation运算符一起使用。@HarishAnchu是的,现在更好了,但单元格仍然没有分开,有些列名以任意顺序重复多次删除for循环中的回显行。前面没有空格
$header .=
<?php

    // Database Connection

    $host="localhost";
    $uname="root";
    $pass="";
    $database = "a2zwebhelp"; 

    $connection=mysql_connect($host,$uname,$pass); 

    echo mysql_error();

    //or die("Database Connection Failed");
    $selectdb=mysql_select_db($database) or 
    die("Database could not be selected"); 
    $result=mysql_select_db($database)
    or die("database cannot be selected <br>");

    // Fetch Record from Database

    $output = "";
    $table = ""; // Enter Your Table Name 
    $sql = mysql_query("select * from $table");
    $columns_total = mysql_num_fields($sql);

    // Get The Field Name

    for ($i = 0; $i < $columns_total; $i++) {
    $heading = mysql_field_name($sql, $i);
    $output .= '"'.$heading.'",';
    }
    $output .="\n";

    // Get Records from the table

    while ($row = mysql_fetch_array($sql)) {
    for ($i = 0; $i < $columns_total; $i++) {
    $output .='"'.$row["$i"].'",';
    }
    $output .="\n";
    }

    // Download the file

    $filename = time().'csv'; //For unique file name
    header('Content-type: application/csv');
    header('Content-Disposition: attachment; filename='.$filename);

    echo $output;
    exit;

    ?>