$checkQuery = "SELECT Id FROM Events
WHERE Date = '$date'
AND VehId = $vehicleId
AND ((StartTime BETWEEN $timeOut AND $timeIn)
OR (EndTime BETWEEN $timeOut AND $timeIn))";
$checkResults = sqlsrv_query($conn, $checkQuery);
if (sqlsrv_num_rows($checkResults) > 0)
{
//do some stuff where the vehicle is already reserved.
} else { // this always executes.
var_dump($rowCounts, $date, $vehicleId, $timeOut, $timeIn);
//insert the new reservation.
}
// Var_dump - bool(false)string(10)" 2016-11-01" int(184)float(9.5)float(11)
我的SQL查询不断返回false,虽然我没有得到我的期望参数是1错误?始终执行else语句。因此,在我的查询中出现了问题,但下面是我的小提琴,表明它按预期工作?我的目标是查询数据库,如果有任何结果返回,它不会插入新记录,因为车辆应该已经预留。任何帮助表示赞赏! http://sqlfiddle.com/#!3/a86be/6
SELECT Id FROM Events
WHERE Date = '2016-11-01'
AND VehId = 184
AND ((StartTime BETWEEN 9.5 AND 11)
OR (EndTime BETWEEN 9.5 AND 11))
我假设显而易见,但我确实确认数据库中确实存在应该返回的记录。
答案 0 :(得分:1)
如果使用前向光标(默认)或动态光标,则返回FALSE。
所以你需要指定一个不同的游标,这样就完成了:
$checkResults = sqlsrv_query(
$conn,
$checkQuery,
array(),
array('Scrollable' => 'buffered'));