我有一个像这样的 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 始终返回总行数。我希望它仅返回当前时间在输入/输出时间内的行。我做错了什么?
最佳答案
您需要一个 where
子句:
SELECT rm.*
from raRmSchedule
WHERE (NOW() > rsintime AND NOW() < rsouttime)
计算行数:
SELECT COUNT(*)
from raRmSchedule
WHERE (NOW() > rsintime AND NOW() < rsouttime)
这将返回一行一列。如果没有匹配,则为 0
。
您的版本为表中的每一行返回一行。将有一列的值为 0
或 1
,具体取决于条件是否匹配。
关于php - 使用 Now() 搜索 MySQL 总是返回 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26580554/