从SQL解析数据时,PHP foreach循环意外停止

时间:2019-05-02 11:00:07

标签: php foreach

我正在从MySQL数据生成一个表,遍历每一行的所有键以生成表头。即使var_dump($ result)显示查询已返回表中的所有35列,此foreach循环(下面的第6行)也会在表的第25列之后停止。 该解决方案可能会引起严重的脸部不适,但我只是无法解决这个问题。

$result = mysqli_query($con,$sql);
if(mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        while($thead_done == 0) {
            echo "<thead>";
            foreach($row as $rkey => $rvalue) {
                echo "<th>";
                switch($rkey) {
[bunch of cases to make the table headers human readable]
                    default:
                        echo $rkey;
                        $s_headers[]='"' . $rkey . '"';
                }
                echo "</th>";
            }
            echo "</thead>";
            $thead_done++;

为确认问题不在于数据库中第25列的内容,我修改了SQL查询以显式选择此列,然后对其进行解析就可以了。 另外,我确定PHP只会在foreach循环内停止,因为返回的HTML结尾为:

<th>[column 25 header]

表示未回显循环最后一行的结束标记。

不幸的是,我无权访问此Web服务器上的PHP日志,这使我更加难以理解为什么它只是停止在该位置。

更新:出于调试目的,我对代码的开头进行了非常小的更改,但其行为相同。完整代码:

$result = mysqli_query($con,$sql);
if(mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        print_r(array_keys($row));
        while($thead_done == 0) {
            echo "<thead>";
            foreach(array_keys($row) as $rkey) {
                echo "<th>";
                switch($rkey) {
                    case "ppower":
                        echo "Plasma Power";
                        $s_headers[]="Plasma Power";
                        break;
                    case "cathodel":
                        echo "Cathode Length";
                        $s_headers[]="Cathode Length";
                        break;
                    default:
                        echo $rkey;
                        $s_headers[]='"' . $rkey . '"';
                }
                echo "</th>";
            }
            echo "</thead>";
            $thead_done++;
            /* Spout XLS GENERATION */
            $writer = WriterFactory::create(Type::XLSX);
            $writer->openToFile($filename);
            $style = (new StyleBuilder())->setFontBold()->build();
            $writer->addRowWithStyle($s_headers, $style);
        }
        unset($s_data);
        echo "<tr>";
        foreach($row as $rkey => $rvalue) {
            if($rkey == "magnet_id") {
                echo "<td><a href=\"details.php?id=" . $rvalue . "\">" . $rvalue . "</td>";
                $s_data[]=$rvalue;
            } else if($rkey =="edms") {
                echo "<td><a href=\"[URL here]" . $rvalue . "/1\" target=\"_blank\">" . $rvalue . "</a></td>";
                $s_edms = '=HYPERLINK("[URL here]' . $rvalue . '","' . $rvalue . '")';
                $s_data[]=$s_edms;
            } else {
                echo "<td>" . $rvalue . "</td>";
                $s_data[]=$rvalue;
            }
        }
        echo "</tr>";
        $writer->addRow($s_data);
    }
    $writer->close();
}

另外,打印$ result数组的所有键可确认返回了所有数据:

  

数组([0] => magnet_id [1] => magnet_type [2] =>原点[3] => Scheduled_time [4] => vc_id [5] =>移除[6] => Removed_time [7] => ir1 [8] =>涂层[9] =>涂层运行[10] =>涂层时间[11] =>系统[12] => sey [13] => ir2 [14] => lastop [15] => edms [16] => pump_time [17] => pressurel [18] => ccomment [19] =>烘烤[20] =>持续时间[21] =>压力[22] => pvoltage [23] => pcurrent [ 24] => ppower [25] =>阴极[26] => pdensity [27] =>修改时间[28] => reinstalled_loc [29] => reinstalled_time [30] =>用户名[31] => sample_id [32] => rfqc [33] => rfqc_time [34] => rfqc_comment)

但是表仍然看起来像这样:

<thead><th>Asset ID</th><th>Asset Type</th><th>Position</th><th>Scheduled</th><th>Vacuum Chamber</th><th>removed</th><th>Removed</th><th>ir1</th><th>Coated</th><th>Coating Run</th><th>Coating Time</th><th>Coating System</th><th>SEY</th><th>ir2</th><th>lastop</th><th>EDMS</th><th>Start Pumping Time</th><th>P-limit</th><th>ccomment</th><th>bakeout</th><th>Proc. Duration</th><th>Proc. Pressure</th><th>Plasma Voltage</th><th>Plasma Current</th><th>Plasma Power
:EOF

0 个答案:

没有答案