创建.csv下载的问题


Problems creating a .csv download

我试图从mysql表中创建。csv下载,但没有多大成功。我把下面的代码从一个教程,我在这里找到http://code.stephenmorley.org/php/creating-downloadable-csv-files/,但由于某种原因,下载的。csv文件包含页面的整个源代码,而不是列标题和数据从mysql表。

任何帮助都将是非常感激的:)

    // 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);
    }

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