删除用php导出的.csv文件的第一行

时间:2014-03-18 06:56:25

标签: php csv

我使用此功能将mysql查询下载到.csv文件中:

<?php

function exportMysqlToCsv($table,$filename = 'ready.txt')
{
    $csv_terminated = "\n";
    $csv_separator = ";";
    $csv_enclosed = '';
    $csv_escaped = "\\";
    $sql_query = "SELECT * from table";

    // Gets the data from the database
    $result = mysql_query($sql_query);
    $fields_cnt = mysql_num_fields($result);


    $schema_insert = '';

    for ($i = 0; $i < $fields_cnt; $i++)
    {
        $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
            stripslashes(mysql_field_name($result, $i))) . $csv_enclosed;
        $schema_insert .= $l;
        $schema_insert .= $csv_separator;
    } // end for

    $out = trim(substr($schema_insert, 0, -1));
    $out .= $csv_terminated;

    // Format the data
    while ($row = mysql_fetch_array($result))
    {
        $schema_insert = '';
        for ($j = 0; $j < $fields_cnt; $j++)
        {
            if ($row[$j] == '0' || $row[$j] != '')
            {

                if ($csv_enclosed == '')
                {
                    $schema_insert .= $row[$j];
                } else
                {
                    $schema_insert .= $csv_enclosed .
                    str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed;
                }
            } else
            {
                $schema_insert .= '';
            }

            if ($j < $fields_cnt - 1)
            {
                $schema_insert .= $csv_separator;
            }
        } // end for

        $out .= $schema_insert;
        $out .= $csv_terminated;
    } // end while

    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Length: " . strlen($out));
    // Output to browser with appropriate mime type, you choose ;)
    header("Content-type: text/x-csv");
    //header("Content-type: text/csv");
    //header("Content-type: application/csv");
    header("Content-Disposition: attachment; filename=$filename");
    echo $out;
    exit;

}

?>

.csv文件已下载,但它有一个标题。我想将此标题从文件中删除。

d;user_id;type;LCASE(f32vav_accounts.account);CAST(AES_DECRYPT(BASE64_DECODE(`password`),'password') AS CHAR)
131;123;dfgn;1234
208;123;herth;fe
97;123;etrher;dl
52;123;trehrd;1234
140;123;regdf;1222
109;123;sdfgsd;33333
43;123;zzxvxc;1221
134;123;cvbncvb;122
13;123;yukgy;11222
52;123;uygkgy;121221
97;123;jhkmygk;dl
125;123;sergsergse;121231
43;123;sergser;1313
63;123;esrgse;6292
140;123;serger;13132321
50;123;ergerg;31241234

97; 123;包缝机; d87695l

如何让脚本省略第一行?

0 个答案:

没有答案