【发布时间】:2021-11-16 22:18:45
【问题描述】:
我正在尝试将 MySQL 表中的数据导出为 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');
不幸的是,当我链接到我的 PHP 文件时,它会下载一个 CSV 文件,该文件只包含列标题,下面没有实际数据,感谢任何帮助。谢谢。
【问题讨论】:
-
使用:客观风格
$rows->fetch_assoc()或程序风格mysqli_fetch_assoc($rows) -
$row = fetch_assoc($rows)应该生成 致命错误:未捕获的错误:调用未定义的函数 fetch_assoc() -
客观意味着面向对象......客观地说:-)