使用Now()搜索MySQL总是返回true

时间:2014-10-27 02:57:28

标签: php mysql datetime

我有一个像这样的MySQL表:

| rsid | rsuser  | rsintime         | rsouttime        | rsroom |
| ---- | ------- | ---------------- | ---------------- | ------ |
|    1 | Nick S  | 10/14/2014 11:17 | 10/14/2014 12:18 |    1   |
|    2 | Mike G  | 10/15/2014 10:18 | 10/15/2014 11:19 |    1   |
|    3 | Chuck M | 10/14/2014 21:56 | 10/14/2014 22:56 |    1   |
|    4 | Jake B  | 10/26/2014 22:14 | 10/26/2014 23:15 |    1   |

我的PHP代码是:

<?php
$con=mysqli_connect("0.0.0.0","roomapp","hi","roomapp");
$testschedulesql = "SELECT (NOW() > rsintime AND NOW() < rsouttime) as res_rm from raRmSchedule";
$testscheduleqry = mysqli_query($con, $testschedulesql);
$testschedule = mysqli_num_rows($testscheduleqry);
$testscheduletext = mysqli_fetch_array($testscheduleqry);
echo $testschedule;
if($testschedule > 0){
    echo 'Busy';
}
else{
    echo 'Not';
}
?>

但是,$ testschedule总是返回总行数。我希望它只返回当前时间在输入/输出时间内的行。我做错了什么?

2 个答案:

答案 0 :(得分:2)

您需要where子句:

SELECT rm.*
from raRmSchedule
WHERE (NOW() > rsintime AND NOW() < rsouttime)

计算行数:

SELECT COUNT(*)
from raRmSchedule
WHERE (NOW() > rsintime AND NOW() < rsouttime)

这将返回一行,其中包含一列。如果没有匹配,它将是0

您的版本为表格中的每一行返回一行。将有一列值为01,具体取决于条件是否匹配。

答案 1 :(得分:0)

当然它会为你提供所有行,因为你选择的是表达式的布尔值。

我认为你想要的是给查询一个WHERE子句来过滤你需要的时间。

喜欢的东西 从raRMSchedule中选择*其中(now()&gt; rsintime和now()&lt; rsouttime)。