SELECT COUNT:在条件下使用mysql_query

时间:2013-11-13 09:18:08

标签: php mysql

我需要检查房间预订网站指定时间段的房间可用性,但具有以下功能;如果房间不可用,则输出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;而不是echo (mysql_result($query, 0));

来返回true或false

4 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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'");

答案 2 :(得分: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."'");

答案 3 :(得分:0)

如何不使用COUNT1约束它的行中选择WHERE而是选择简单值COALESCE,然后使用0默认为{{1如果没有返回结果:

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)