创建.csv下载时出现问题

时间:2013-02-28 08:12:19

标签: php mysql csv download

我正在尝试从mysql表创建.csv下载但没有太大成功。我从下面的http://code.stephenmorley.org/php/creating-downloadable-csv-files/教程中汇总了下面的代码,但由于某种原因,下载的.csv文件包含页面的完整源代码,而不是列标题和mysql表中的数据。< / p>

非常感谢任何帮助:)

    // output headers so that the file is downloaded rather than displayed
    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename=site-list-export.csv');

    // create a file pointer connected to the output stream
    $output = fopen('php://output', 'w');

    // output the column headings
    fputcsv($output, array('id', 'url', 'clean_host', 'keyword', 'group', 'page_number', 'page_authority', 'domain_authority', 'page_mozrank', 'seomoz', 'page_title', 'check_inbound', 'check_inbound_priority ', 'count', 'project', 'type'));


    // fetch the data
    $connect = mysql_connect("localhost", DB_USERNAME, DB_PASSWORD);
    mysql_select_db(DB_DATABASE, $connect);

    $rows = mysql_query("SELECT * 
    FROM `url_list_google`
    WHERE `group` = '".$selected_group."' 
    GROUP BY clean_host 
    ORDER BY `domain_authority` DESC") or die(mysql_error());
    mysql_close($connect);

    // loop over the rows, outputting them
    while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row);

    }

1 个答案:

答案 0 :(得分:0)

在循环后添加exit;函数调用,它将阻止其他内容呈现。

相关问题