IlanaWexler
IlanaWexler

Reputation: 17

Results not displaying in PHP

My problem is: results are not displaying in my demo-results.php from the data entered in a demo.php. The data were stored in my database but the javascript alert '0 results' always displays after the demo.php page redirected me to demo-results.php page:

this is the php portion of my demo-results page:

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

Please help me display the Water Reading, the Water Usage and the Payment. Thank you.

Upvotes: 0

Views: 56

Answers (1)

devpro
devpro

Reputation: 16117

You are using LIMIT in your query:

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;

And using this condition:

if ($result->num_rows > 2) {

Your query will return only one record because you are using LIMIT 1 than this condition if ($result->num_rows > 2) { will always failed.

Solution:

If you want to use LIMIT 1 than you can use condition as like:

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

Upvotes: 4

Related Questions