drctyr2869 2016-07-09 03:47
浏览 48
已采纳

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

I am having problems exporting data from a MySQL database using a PHP script that mimes an excel file. The data is only exporting the last row of the MySQL table. I have chopped the code down to remove all the relational look ups (as there are multiple MySQL queries through out which make it hard to read). I understand I am over writing my variables so only the last row of selected is available to the script but after a lot of searching I still cant seem to find an answer (I am guessing I need to store the data in an array then call that array within the code that exports the data as an excel file). All help will be greatly appreciated. My code (chopped down version) is:

<?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;
?>

Many thanks in advance.

Cheers Jase

PS I worked this code up by searching the web over the last few days and put it together from that prior to this I have never worked on this type of stuff (outputting file types)

  • 写回答

1条回答 默认 最新

  • dqwh1219 2016-07-09 06:02
    关注

    Your code could use a lot of cleanup, but I will let you figure that out later, and focus on making it work as you have intended.

    You can do this by using concatenation .=

    //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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能