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

时间:2014-11-05 12:03:12

标签: php mysql mysqli conditional

这次我试图制作一些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:我还在努力学习英语,所以如果我失言了,请告诉我,谢谢你。

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

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

非常接近使其成功,感谢

1 个答案:

答案 0 :(得分:1)

更新:因为您还更新了OP,现在我找到了导致问题的原因。当你说:die('Could not get data: ' . mysqli_error());之后你会想要尝试一个while循环。虽然不会执行,但是您使用die();终止了脚本,将while移至else条件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);