php代码在导出到excel时重复行

标签 php mysql excel

我必须遵循代码,当它运行时,它会重复空白行,或者会重复其他充满数据的行,我明白为什么吗?

$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/

相关文章:

php - 如何在php中显示带有列名的json数据

mysql - 通过连接一列从不同的表返回两列

php - MySQL 查询返回匹配的记录,但也返回不匹配的记录

用于打开带有图表的现有 .xlsx 工作簿的 Python 库

excel - 如何更改Excel VSTO加载项的显示名称?

php - Guzzle HTTP 客户端比 Symfony HTTP 客户端慢

php - swiftmailer Swift_TransportException 未被 try-catch block 捕获

php - 在 AngularJS 中捕获 PDO 异常

mysql - 我可以从 mysql 5.5 打开 mysql 5.1 数据库吗?

excel - "Pivot"Excel 中的表格