我正在尝试从 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);
}