我的问题是:我的demo-results.php中的结果没有显示在demo.php中输入的数据中。数据存储在我的数据库中,但是javascript警告' 0结果'在demo.php页面将我重定向到demo-results.php页面后始终显示:
这是我的演示结果页面的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();
?>
请帮我显示水阅读,用水和付款。谢谢。
答案 0 :(得分:4)
您在查询中使用LIMIT:
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
,而此条件if ($result->num_rows > 2) {
始终会失败。
<强>解决方案:强>
如果您想使用LIMIT 1
,则可以使用条件:
if ($result->num_rows > 0) { // if no of rows found.