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

时间:2015-08-18 15:05:23

标签: 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);

1 个答案:

答案 0 :(得分:0)

由于您使用的是 <div id="graph" style="height: 400px"></div> ,如果满足以下条件之一,您的结果中会有多行:

LEFT OUTER JOIN

您可以通过将查询更改为以下内容来解决此问题:

The values in table fpo, column empnum are not unique
The values in table users, column empnum are not unique

但是如果没有关于您的数据的更多信息,这只是猜测......