PHP Mysql创建一个Pipe delimeted输出文件

时间:2015-11-28 13:55:55

标签: php mysql

我使用下面的csv导出,但我想导出为管道delimeted。

function to_CSV($table) {

    $file_csv = "file_csv.csv";
    $fp = fopen('php://output', 'w'); 

    $query = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='htmltable' AND TABLE_NAME='$table'";
    $result = mysqli_query(db_connect(),$query);

    while ($row = mysqli_fetch_row($result)) {
            $header[] = $row[0];
    }

    header('Content-type: application/csv');
    header('Content-Disposition: attachment; filename='.$file_csv);
    fputcsv($fp, $header);

    $query  ="SELECT * from $table";
    $result = mysqli_query(db_connect(),$query);

    while($row = mysqli_fetch_row($result)) {
            fputcsv($fp, $row);
    }
    exit;
}

请帮帮我

2 个答案:

答案 0 :(得分:1)

fputcsv接受自定义分隔符作为第三个参数。见http://php.net/manual/en/function.fputcsv.php

例如:

while($row = mysqli_fetch_row($result)) {
    fputcsv($fp, $row, '|');
}

答案 1 :(得分:0)

fputcsv允许您指定分隔符

fputcsv($fp, $header,  "|");

请参阅http://php.net/manual/en/function.fputcsv.php