-1

我想使用数据库中的数据生成一个 csv 文件。如何在 PHP CodeIgniter 框架中做到这一点?

4

2 回答 2

1
function query_to_csv( $query, $filename, $attachment = false, $headers = true) {

    if($attachment) {
        // send response headers to the browser
        header( 'Content-Type: text/csv' );
        header( 'Content-Disposition: attachment;filename='.$filename);
        $fp = fopen('php://output', 'w');
    } else {
        $fp = fopen($filename, 'w');
    }

    $result = mysql_query($query);

    if($headers) {
        // output header row (if at least one row exists)
        $row = mysql_fetch_assoc($result);
        if($row) {
            fputcsv($fp, array_keys($row));
            // reset pointer back to beginning
            mysql_data_seek($result, 0);
        }
    }

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

    fclose($fp);
    exit;
}
于 2013-10-30T06:20:50.163 回答
1

看看我的这个答案,您将确切地知道如何做到这一点。

Codeigniter 中的报告

您需要使用Codeigniter 数据库实用程序类

并且需要返回查询而不是result_arrayresult

于 2013-10-30T06:41:09.550 回答