-1

我需要在房间预订网站上查看指定时间段内的房间供应情况,但具有以下功能;如果房间不可用,则输出 101,如果可用则输出 000;但我希望它只是 1 或 0。

function room_unavailable($room_name, $s_at, $e_at){

    $room_name = sanitize($room_name);
//  $s_at = sanitize();
//  $e_at = sanitize();

    $query = mysql_query("SELECT COUNT('room_name') FROM `ordered_rooms` WHERE (`s_at` BETWEEN 7 AND '$e_at') AND (`e_at` BETWEEN '$s_at' AND 18)AND `room_name`= '$room_name'");
    echo (mysql_result($query, 0)); 

}

因为我需要使用return (mysql_result($q, 0)== 1) ? true: false;, 而不是返回 true 或 falseecho (mysql_result($query, 0));

4

4 回答 4

0

我希望这对你有用

function room_unavailable($room_name, $s_at, $e_at){

    $room_name = sanitize($room_name);
//  $s_at = sanitize();
//  $e_at = sanitize();

    $query = mysql_query("SELECT COUNT('room_name') FROM `ordered_rooms` WHERE (`s_at` BETWEEN 7 AND '$e_at') AND (`e_at` BETWEEN '$s_at' AND 18)AND `room_name`= '$room_name'");

    $res=mysql_result($query, 0); //if this holds 000 or 101
    $res=substr($res,0,1);
    echo $res; // Prints either 1 or 0 depending upon your availability result.

}

另一种方法..

代替

return (mysql_result($q, 0)== 1) ? true: false;

采用

return (mysql_result($q, 0)== 1) ? 1:0;
于 2013-11-13T09:22:03.013 回答
0
   $query = mysql_query("SELECT COUNT('room_name') FROM `ordered_rooms` WHERE (`s_at` > 7
   AND `s_at` < '$e_at') AND (`e_at` > '$s_at' AND `e_at` < 18) AND `room_name` LIKE 
   '$room_name'");
于 2013-11-13T09:31:53.873 回答
0

试试这个:

$query = mysql_query("SELECT COUNT('room_name') FROM `ordered_rooms` WHERE (`s_at` BETWEEN 7 AND '".$e_at."') AND (`e_at` BETWEEN '".$s_at."' AND 18)AND `room_name`= '".$room_name."'");
于 2013-11-13T09:36:01.870 回答
0

如果没有返回结果,如何不选择 a但从您将其约束到的行COUNT中的简单值,然后使用 a默认为 :1WHERECOALESCE0

SELECT COALESCE((SELECT 1 FROM `ordered_rooms` WHERE (`s_at` BETWEEN 7 AND '$e_at') AND (`e_at` BETWEEN '$s_at' AND 18)AND `room_name`= '$room_name')), 0)
于 2013-11-13T09:37:04.667 回答