php while循环在外壳中运行,但不在浏览器中运行

时间:2018-06-24 01:15:38

标签: php

我做了一个非常简单的PHP7页面,该页面在表格中显示温度传感器的值。我从MySQL数据库提取此数据。 PHP7在raspberrypi 3上与apache2一起运行。

这一切都很好,我能够使代码在CLI上运行,并且可以生成HTML。但是,当我在浏览器中运行页面时,它仅显示while循环之前和之后的内容。所有其他PHP / HTML都显示正常。文件名为tempsense.php

<html>
<head>
    <title>
        TEMP 
    </title>
</head>
<body>
    <a href="index.html">HOME</a>
    <?php
        $con = mysqli_connect("localhost","root","*********");
        $db = mysqli_select_db($con,"connected_sensors");
        $query = "SELECT * FROM sensors";
        $result = mysqli_query($con, $query);
        echo "<table border='1'>";
        echo "<tr>";
        echo "<th>Sensor ID</th><th>Sensor Name</th><th>Temp F</th>";
        echo "</tr>";

        while ($row = mysqli_fetch_array($result))
        {
            echo "<tr>";
            echo "<td>", $row['sensor_id'], "</td><td>", $row['sensor_name'], "</td><td>", $row['sensor_value'], "</td>";
            echo "</tr>";
        }
        echo "</table>";
        mysqli_close($con);
    ?>
</body>

浏览器的来源是

<html>
<head>
    <title>
        TEMP
    </title>
</head>
<body>
    <a href="index.html">HOME</a>
    <table border='1'><tr><th>Sensor ID</th><th>Sensor Name</th><th>Temp F</th></tr></table>    </body>

并通过PHP CLI

<html>
<head>
    <title>
        TEMP
    </title>
</head>
<body>
    <a href="index.html">HOME</a>
    <table border='1'><tr><th>Sensor ID</th><th>Sensor Name</th><th>Temp F</th></tr><tr><td>28-0517c15db7ff</td><td>Device_0</td><td>79.5866</td></tr><tr><td>28-0117b3e303ff</td><td>Device_1</td><td>79.1366</td></tr></table>    </body>

我知道这可能是愚蠢的,但我正在失去理智。 预先感谢。

0 个答案:

没有答案