使用PHP导出mysql表到CSV - 标题问题

时间:2012-12-12 11:36:44

标签: php mysql header export

  

可能重复:
  Headers already sent by PHP

将mysql表转换为.CSV文件的PHP脚本给出了错误,例如无法修改标题信息而不是将数据写入CSV文件:

以下是错误:

  

警告:无法修改标题信息 - 第30行/home/public_html/amgtst/export-tst.php中已经发送的标题(在/home/public_html/amgtst/export-tst.php:3处开始输出)< / p>      

警告:无法修改标题信息 - 第31行/home/public_html/amgtst/export-tst.php已经发送的标题(在/home/public_html/amgtst/export-tst.php:3处开始输出)< / p>      

警告:无法修改标题信息 - 第32行/home/public_html/amgtst/export-tst.php已经发送的标题(在/home/public_html/amgtst/export-tst.php:3处开始输出)< / p>      

警告:无法修改标题信息 - 第33行/home/public_html/amgtst/export-tst.php已经发送的标题(在/home/public_html/amgtst/export-tst.php:3处开始输出)< / p>

Area Area DC_No Area DC_No Product_type Area DC_No Product_type DC_date Area DC_No Product_type DC_date Ac_code Area DC_No Product_type DC_date Ac_code PO_No Area DC_No Product_type DC_date Ac_code PO_No PO_date Area DC_No Product_type DC_date Ac_code PO_No PO_date Inv_No Area DC_No Product_type DC_date Ac_code PO_No PO_date Inv_No CNo_Qty Area DC_No Product_type DC_date Ac_code PO_No PO_date Inv_No CNo_Qty Deleted Area DC_No Product_type DC_date Ac_code PO_No PO_date Inv_No CNo_Qty Deleted Vehicle_no Area DC_No Product_type DC_date Ac_code PO_No PO_date Inv_No CNo_Qty Deleted Vehicle_no "Jayanagar" "100" "Gas" "2012-11-30" "2" "2012-11-30" "11" "KA123-333" "Jayanagar" "104" "Gas" "2012-12-03" "3" "2012-12-03" "14" "Jayanagar" "101" "Gas" "2012-12-03" "3" "2012-12-03" "13" "Jayanagar" "105" "Gas" "2012-12-03" "3" "2012-12-03" "17" "Jayanagar" "106" "Gas" "2012-12-03" "3" "2012-12-03" "16" "Jayanagar" "107" "Gas" "2012-12-03" "3" "2012-12-03" "KA576" "Jayanagar" "108" "Gas" "2012-12-03" "2" "2012-12-03" "25" "KA01P213" "Jayanagar" "111" "Gas" "2012-12-04" "2" "2012-12-04" "27" "Jayanagar" "125" "Gas" "2012-12-04" "3" "2012-12-04" "12" "Jayanagar" "116" "Gas" "2012-12-06" "2" "2012-12-06" "Jayanagar" "117" "Gas" "2012-12-06" "2" "2012-12-06" "19" "Jayanagar" "118" "Gas" "2012-12-06" "2" "2012-12-06" "20" "Jayanagar" "119" "Gas" "2012-12-06" "2" "2012-12-06" "21" "Jayanagar" "130" "Gas" "2012-12-06" "3" "2012-12-06" "22" "KA-01-A4564" "Jayanagar" "131" "Gas" "2012-12-08" "2" "2012-12-08" "23" "KA01-23212" "Jayanagar" "132" "Gas" "2012-12-08" "2" "PIA-234234-ERES" "2012-12-08" "24" "KA-10-23232" "Jayanagar" "133" "Gas" "2012-12-08" "2" "verbal" "2012-12-08" "26" "ka91901212" 

我的php脚本:

    <?php
    include 'dbconnect.php';

    echo "Exporting file - process"."<br><br>";


        header("Content-type: application/csv");
        header("Content-Disposition: attachment; filename=download.csv");
        header("Pragma: no-cache");
        header("Expires: 0");



    $query = "SELECT * FROM DCHDR";

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

    php?>

4 个答案:

答案 0 :(得分:1)

在这一行

echo "Exporting file - process"."<br><br>";

你做了一些输出,而几行之后你会尝试几次header()次呼叫。

引用PHP docu

  

请记住,在发送任何实际输出之前,必须通过普通HTML标记,文件中的空行或PHP来调用header()。

在您创建向客户端发送一些输出(使用echo或类似内容)之前,请执行任何标头调用!

除此之外,您还可以使用原生fputcsv()来创建CSV文件。如果您想将输出发送到客户端,可能与tmpfile()结合使用。

答案 1 :(得分:0)

使用ob_start();函数位于文件顶部并删除所有回声期望最后一个

答案 2 :(得分:0)

删除

echo "Exporting file - process"."<br><br>";

答案 3 :(得分:0)

修改:结帐this question

您需要使用ob_start();然后使用ob_end_flush(),因为看起来您尝试在已经将某些内容发送到浏览器后设置标头。

试试这个:

<?php

ob_start();

include 'dbconnect.php';

echo "Exporting file - process"."<br><br>";


    header("Content-type: application/csv");
    header("Content-Disposition: attachment; filename=download.csv");
    header("Pragma: no-cache");
    header("Expires: 0");



$query = "SELECT * FROM DCHDR";

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

ob_end_flush();

exit();

php?>

您也可以使用php的内置CSV功能fputcsv