php - 有条件 mysqli 日期范围检查以在 sql 中打印价格

标签 php mysql mysqli conditional-statements

这次我试图编写一些 php 代码来与 mysqli 一起使用,以检查今天的日期是否在 Mysql 表中的日期范围之间,如果条件为真,我需要从表中打印价格否则它应该打印与另一个表不同的价格。所以我已经在另一个 php 文件中设置了所有 sql 连接,问题是当我尝试代码时,它什么也没有显示,只显示一个空白页面。这是我使用的代码:

<?php
$currentdate = date("Y/m/d");                               
//basic include files
require_once('/home/user/public_html/folder/db.php');

$seasonalpricedate = mysqli_query($conn, "SELECT `seasonal_price` FROM `hotel_seasonal_price` WHERE room_type_id = '1' AND $currentdate >= 'seasonal_from' AND $currentdate <= 'seasonal_to';");
$result = ($seasonalpricedate) or die(mysqli_error());

if (mysqli_num_rows($result) != 0) {
    $standardprice = mysqli_query($conn, "SELECT `room_price` FROM `hotel_room_price` WHERE price_id = '1'");
    if(! $standardprice ){
        die('Could not get data: ' . mysqli_error());
    }

    while($standard = mysqli_fetch_array($standardprice, MYSQL_ASSOC)){
        echo "$ {$standard['room_price']} ";
    }
} else {
    if(! $seasonalpricedate ){
        die('Could not get data: ' . mysqli_error());

        while($standard2 = mysqli_fetch_array($seasonalpricedate, MYSQL_ASSOC)){
            echo "$ {$standard2['seasonal_price']} ";
        }
    }
}
?>

我已经尝试过在没有条件的情况下使用标准价格和季节性价格的两种代码,但是当我尝试这样做时,它不会显示任何内容。

PostData:我还在努力学习英语,所以如果我不及格的话请原谅我,提前谢谢。

更新:好的,这样如果没有值 true 就可以了,它显示标准价格,但如果匹配日期,则不显示任何内容,这里是更改的代码:

<?php
error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
$currentdate = date("Y-m-d");                               
//basic include files
require_once('/home/trankilo/public_html/book/db.php');

$seasonalpricedate = mysqli_query($conn, "SELECT `seasonal_price` FROM `hotel_seasonal_price` WHERE room_type_id = '1' AND '$currentdate' >= seasonal_from AND '$currentdate' <= seasonal_to");
$result = ($seasonalpricedate) or die(mysqli_error());
if (mysqli_num_rows($result) != 0) {
$seasonalprice = mysqli_query($conn, "SELECT `seasonal_price` FROM `hotel_seasonal_price` WHERE room_type_id = '1'");
if(! $seasonalprice )
{
  die('Could not get data: ' . mysqli_error());
while($standard2 = mysqli_fetch_array($seasonalprice, MYSQL_ASSOC))
{
    echo "$ {$standard2['seasonal_price']} ";
}
}

} else {
$standardprice = mysqli_query($conn, "SELECT `room_price` FROM `hotel_room_price` WHERE price_id = '1'");
if(! $standardprice )
{
  die('Could not get data: ' . mysqli_error());
}
while($standard = mysqli_fetch_array($standardprice, MYSQL_ASSOC))
{
    echo "$ {$standard['room_price']} ";
}
}
mysqli_close($conn);
?>

非常接近让它发挥作用,感谢

最佳答案

更新:因为你也更新了你的OP,现在我找到了导致问题的原因。问题是当你说: die('Could not get data: ' . mysqli_error());之后你想尝试 while环形。 while 不会执行,因为您使用 die(); 终止了脚本移动whileelse您的情况if健康)状况。请参阅我的评论。

使用这个:

error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

//Also display your errors.
display_errors(true);

$currentdate = date("Y-m-d");
//basic include files
require_once('/home/trankilo/public_html/book/db.php');

//Put your query into a variable, so you can dump / print it.
$sql = "SELECT `seasonal_price`"
    . " FROM `hotel_seasonal_price`"
    . " WHERE room_type_id = '1'"
    . " AND '" . $currentdate . "' >= seasonal_from"
    . " AND '" . $currentdate . "' <= 'seasonal_to'";
echo $sql;
//Try to run it in the sql directly. Is it gives back you any result?
//Do not need to 
$result = mysqli_query($conn, $sql) or die(mysqli_error());

if (mysqli_num_rows($result) != 0) {
    //Check if we have result by echoing some dummy text
    echo "Yes, we have result!";

    $sql = "SELECT `seasonal_price` FROM `hotel_seasonal_price` WHERE room_type_id = '1'";
    //Do the same as the previous query. Does it gives you back anything?
    $seasonalprice = mysqli_query($conn, $sql);
    if (!$seasonalprice) {
        //I do not really get what happens here. If you have no seasonalprice, 
        //then you can not fetch_array on that!
        //Move this whole section.... You've say die, and after that do a while?
        die('Could not get data: ' . mysqli_error());
    } else {
        while ($standard2 = mysqli_fetch_assoc($seasonalprice)) {
            echo "$ " . $standard2['seasonal_price'] . " ";
        }
    }
} else {
    //Same as previous
    $sql = "SELECT `room_price` FROM `hotel_room_price` WHERE price_id = '1'";
    $standardprice = mysqli_query($conn, $sql);
    if (!$standardprice) {
        die('Could not get data: ' . mysqli_error());
        //same here, move the while to the else...
    } else {
        while ($standard = mysqli_fetch_array($standardprice, MYSQL_ASSOC)) {
            echo "$ {$standard['room_price']} ";
        }
    }
}
mysqli_close($conn);

关于php - 有条件 mysqli 日期范围检查以在 sql 中打印价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26756761/

相关文章:

php - 使用php将Sql行放入Json?

php - <表单操作="<?=$_SERVER[' PHP_SELF']? >"method="帖子">

php - MySql 数据库结构按顺序存储项目列表

php - 想要删除特定行。如何获取ID?

PHP7 - MySQLI 将站点移动到新服务器后的错误处理问题

php - 在取消设置 mysql 对象之前释放结果并关闭连接,或者只是取消设置会破坏对象 n 释放资源?

php - 在 PHP 中启用 json_encode

javascript - 下拉选择时如何更改div背景颜色?

php - 如何在 laravel-validation-rules/credit-card 中获取验证消息

mysql - mariaDB 查看哪些用户被删除