PHP Excel导出只显示MYsql表的最后一行

我有问题从MySQL数据库使用模仿Excel文件的PHP脚本导出数据。 数据只是导出MySQL表的最后一行。 我删除了所有的关系查找代码(因为有多个MySQL查询通过这使得它很难阅读)。 我知道我已经写了我的variables,所以只有选中的最后一行可用于脚本,但经过大量的search,我仍然无法find答案(我猜我需要将数据存储在一个数组,然后调用将数据导出为ex​​cel文件的代码中的数组)。 所有的帮助将不胜感激。 我的代码(切碎的版本)是:

<?php // Apply server security settings and keep cookies // connecting to the server // selecting the appropriate database //storing and fetching user data $generate_query = "SELECT * FROM main_report"; $generate_data = mysql_query($generate_query, $link); while($report = mysql_fetch_array($generate_data)) { $reportnoout = "<td>".$report['report_number']."</td>"; $incdateout = "<td>".$report['incident_time']."</td>"; $siteout = "<td>".$site_data['site_name']."</td>"; $deptout = "<td>".$dept_data['department_name']."</td>"; $reportout = " <td>".$report['report_type']."</td>"; $superout = "<td>".$staff_data5['name']."</td>"; $descout = "<td>".$report['detailed_desc']."</td>"; // Needs some form of array declaration here maybe? } // filename for download $filename = "test_data_" . date('Ymd') . ".xls"; header("Content-type: application/vnd.ms-excel"); header("Content-Disposition: attachment; filename=$filename"); $test="<table><th>Report No.</th><th>Incident Date</th><th>Site</th><th>Department</th><th>Incident Type</th><th>Responsible Supervisor</th><th>Description</th><tr>"; $test2="$reportnoout $incdateout $siteout $deptout $reportout $superout $descout"; // This is not right either should probably be an array or not even here? echo $test; echo $test2; // This was a stop gap to at least see if some of the code worked exit; ?> 

提前谢谢了。

干杯贾斯

PS我通过searchnetworking过去几天工作这个代码,把它放在一起从此之前,我从来没有在这种types的东西(输出文件types)

你的代码可以使用大量的清理工作,但是稍后我会让你弄清楚,并且专注于按照你的意图工作。

你可以通过使用连接来完成.=

 //start table string $table = "<table><tr> <th>Report No.</th> <th>Incident Date</th> <th>Site</th> <th>Department</th> <th>Incident Type</th> <th>Responsible Supervisor</th> <th>Description</th><tr>"; $generate_query = "SELECT * FROM main_report"; $generate_data = mysql_query($generate_query, $link); while($report = mysql_fetch_array($generate_data)) { //add row to string using concatenation $table .= "<tr><td>{$report['report_number']}</td> <td>{$report['incident_time']}</td> <td>{$site_data['site_name']}</td> <td>{$dept_data['department_name']}</td> <td>{$report['report_type']}</td> <td>{$staff_data5['name']}</td> <td>{$report['detailed_desc']}</td></tr>"; } //close table $table .="</table>"; // filename for download $filename = "test_data_" . date('Ymd') . ".xls"; header("Content-type: application/vnd.ms-excel"); header("Content-Disposition: attachment; filename=$filename"); echo $table; 
    Interesting Posts