【问题标题】:Conditional mysqli date range check to print a price in sql条件 mysqli 日期范围检查以在 sql 中打印价格
【发布时间】:2015-01-01 14:51:29
【问题描述】:

这一次我试图让一些 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);
?>

感谢

【问题讨论】:

    标签: php mysql mysqli conditional


    【解决方案1】:

    更新:因为您也更新了您的 OP,所以现在我找到了导致问题的原因。当你说:die('Could not get data: ' . mysqli_error()); 之后,你想尝试一个while 循环。 while 不会执行,因为您使用 die(); 终止了脚本 将 while 移动到 else 情况下的 if 条件。看我的cmets。

    使用这个:

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

    【讨论】:

    • 非常感谢,这很有帮助,而且效果很好,我通过第二次更新编辑了您的答案,只做了一些更改,非常感谢您的帮助!我还有很多东西要学。
    • 我删除了您的编辑,因为存在语法错误。无论如何,不​​客气!
    猜你喜欢
    • 2016-06-10
    • 2016-11-29
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多