我尝试生成.csv文件,但我将此文件清空。 所以我的代码是:
$fileName = 'Users_' . date('d-m-Y') . '.csv';
$fh = fopen($fileName, "w");
fputcsv($fh, array('EMAIL', 'NAME','SURNAME'), ";");
foreach ($aDataBases as $database) {
$sSql = sprintf('SELECT email, name, surname FROM users');
$rResult = Mysqli::query($sSql, $database);
while ($aRecord = $rResult->fetch_array(MYSQLI_ASSOC)){
fputcsv($fh, $aRecord['email'], ";");
fputcsv($fh, $aRecord['name'], ";");
fputcsv($fh, $aRecord['surname'], ";",'"');
}
}
fclose($fh);
文件生成但是空了,你能帮帮我吗?我创建了一个print_r(),并在查询执行后有数据。我做错了什么?
答案 0 :(得分:3)
您必须将此行更改为
while ($aRecord = $rResult->fetch_array(MYSQLI_ASSOC)){
fputcsv($fh, $aRecord['email'], ";");
fputcsv($fh, $aRecord['name'], ";");
fputcsv($fh, $aRecord['surname'], ";",'"');
}
到
while ($aRecord = $rResult->fetch_array(MYSQLI_ASSOC)){
fputcsv($fh, [$aRecord['email'], $aRecord['name'], $aRecord['surname']]);
}
点击此处http://php.net/manual/en/function.fputcsv.php#refsect1-function.fputcsv-parameters second parameter should be an array
您缺少的
答案 1 :(得分:1)
fputcsv
的第二个参数应为array
,但在您的代码示例中,它是string
。
答案 2 :(得分:0)
fputcsv的第二个参数是一个数组。
你可以这样做:
$fields = [];
while ($aRecord = $rResult->fetch_array(MYSQLI_ASSOC)){
$fields[] = $aRecord['email'];
$fields[] = $aRecord['name'];
$fields[] = $aRecord['surname'];
fputcsv($fh, $fields);
}