我必须遵循代码,当它运行时,它会重复空白行,或者会重复其他充满数据的行,我明白为什么吗?
$filename = date("Y-m-d_g-i-a");
// output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="'.$filename.'.csv"');
// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
// output the column headings
fputcsv($output, array('ID', 'P.O #', 'Date', 'To', 'Time', 'Vin', 'Reason', 'Amount', 'Employee #', 'Mananger', 'Account #', 'Store Location', 'Borrowed', 'verified','Adjustment','Adjustment by','','VOID','Emp Name'));
// fetch the data
mysql_connect('******', '*******', '******');
mysql_select_db('********');
$rows = mysql_query('SELECT fpo.*, users.name FROM fpo LEFT OUTER JOIN users ON (fpo.empnum = users.empnum)');
// loop over the rows, outputting them
while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row);
最佳答案
由于您使用的是LEFT OUTER JOIN
,如果满足以下条件之一,您将在结果中获得多行:
The values in table fpo, column empnum are not unique
The values in table users, column empnum are not unique
您可以通过将查询更改为如下所示来解决此问题:
SELECT fpo.*, users.name
FROM fpo
LEFT OUTER JOIN users
ON (fpo.empnum = users.empnum)')
GROUP BY users.name
;
但是,如果没有有关您数据的更多信息,这只是猜测......
关于php代码在导出到excel时重复行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32076094/