结果不显示在PHP中

结果不显示在PHP中,php,mysql,Php,Mysql,我的问题是:从demo.php中输入的数据来看,结果没有显示在我的demo-results.php中。数据存储在我的数据库中,但在demo.php页面将我重定向到demo-results.php页面后,javascript警报“0 results”始终显示: 这是我的演示结果页面的php部分: <div class="col-lg-12 text-center"> <hr cla

我的问题是:从demo.php中输入的数据来看,结果没有显示在我的demo-results.php中。数据存储在我的数据库中,但在demo.php页面将我重定向到demo-results.php页面后,javascript警报“0 results”始终显示:

这是我的演示结果页面的php部分:

                      <div class="col-lg-12 text-center">
                            <hr class="visible-xs">
                            <h2>Water Reading is: </h2>
                            <hr class="visible-xs">

            <?php 
                    $servername = "localhost";
                    $username = "root";
                    $password = "";
                    $dbname = "water";

                    // Create connection
                    $conn = new mysqli($servername, $username, $password, $dbname);
                    // Check connection
                    if ($conn->connect_error) {
                        die("Connection failed: " . $conn->connect_error);
                    }

                    $sql = "SELECT first_number, second_number, third_number, fourth_number, fifth_number, sixth_number, prev_reading, pres_reading FROM demo_numbers ORDER BY post_id DESC LIMIT 1;";

                    $result = $conn->query($sql);

                    if ($result->num_rows > 2) {
                        // output data of each row
                        while($row = $result->fetch_assoc()) {

                ?>


                        <strong><?php echo $row["first_number"]. $row["second_number"] . $row["third_number"]. $row["fourth_number"] . $row["fifth_number"]. $row["sixth_number"] . "<br>";?></strong>

                            <hr class="visible-xs">
                            <h2>Your Water Usage is: </h2>
                            <hr class="visible-xs">
                            <strong>
                <?php 
                        $int_pres = intval($row["pres_reading"]);
                        $int_prev = intval($row["prev_reading"]); 
                        $difference = $int_pres-$int_prev;
                        $payment = $difference*26.678;      

                        echo $difference . " cu. m." . "<br>"; 

                        }
                ?></strong>  

                <hr class="visible-xs">
                            <h2>Your Payment is approximately: </h2>
                            <hr class="visible-xs">
                            <strong>
                <?php 


                         echo "Php " . $difference . "<br>"; ?></strong> 
                <?php
                    } else {
                        echo "<script>alert('0 results')</script>";
                    }

                    $conn->close();
                ?>


水读数为:

你的用水是:

您的付款约为:

您正在查询中使用限制:

SELECT first_number, second_number, third_number, fourth_number, fifth_number, sixth_number, prev_reading, pres_reading FROM demo_numbers ORDER BY post_id DESC LIMIT 1;
使用这个条件:

if ($result->num_rows > 2) {
您的查询将只返回一条记录,因为您使用的是
LIMIT 1
,如果($result->num_rows>2),则此条件
将始终失败

解决方案:

如果要使用
限制1
,可以使用如下条件:

if ($result->num_rows > 0) { // if no of rows found.

您正在查询中使用限制:

SELECT first_number, second_number, third_number, fourth_number, fifth_number, sixth_number, prev_reading, pres_reading FROM demo_numbers ORDER BY post_id DESC LIMIT 1;
使用这个条件:

if ($result->num_rows > 2) {
您的查询将只返回一条记录,因为您使用的是
LIMIT 1
,如果($result->num_rows>2),则此条件
将始终失败

解决方案:

如果要使用
限制1
,可以使用如下条件:

if ($result->num_rows > 0) { // if no of rows found.

也许应该是
if($result->num_rows>0){
如果您限制查询只返回一个结果行,为什么要在WHILE循环中处理单个结果行?也许应该是
if($result->num_rows>0){
另外,如果您限制查询只返回一个结果行,为什么要在WHILE循环中处理单个结果行?我在查询中使用LIMIT,因为我只想显示最新一行的数据。@ChrisJustin:检查我答案中的解决方案部分。@IlanaWexler:很高兴帮助您,别忘了接受这个答案我使用L我的查询中有限制,因为我只想显示最新一行的数据。@ChrisJustin:检查我答案中的解决方案部分。@IlanaWexler:很高兴帮助你,别忘了接受这个答案