fputcsv 仅创建列 headers

fputcsv creating only column headers

我正在尝试将数据从 MySQL table 导出为 CSV,我的客户需要定期通过电子邮件将其发送出去。

这是我的 PHP 文件包含的内容:

require('db.php');

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

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

// output the column headings
fputcsv($output, array('CLUB NAME', 'NUMBER', 'TITLE', 'TYPE', 'FORENAME', 'SURNAME', 'ADDRESS1', 'ADDRESS2', 'ADDRESS3', 'ADDRESS4', 'POSTCODE', 'PHONE', 'EMAIL', 'MOBILE'));

//create query
$sql = "SELECT Club_Name,BHS,Title,Type,Forename,Surname,Address1,Address2,Address3,Address4,Postcode,Phone,Email,Mobile FROM tvrc_members WHERE Current = '1'";
$rows = $conn->query($sql);

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

require('foot.php');

不幸的是,当我 link 我的 PHP 文件时,它下载了一个 CSV 文件,该文件仅包含 headers 列,下面没有实际数据,感谢您的帮助。谢谢

根据@berend 的评论,我将最后一行更改为

while ($row = $rows->fetch_assoc()) {fputcsv($output, $row);}

这行得通。