ShaneW ShaneW - 1 month ago 19
PHP Question

SQL Returning False

$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)

My SQL query continually returns false, Though I do not get my expects param to be 1 error? Always executes the else statement. Thus, something is wrong in my query, but below is my fiddle that shows it working as expected? My goal is to query the db, if any results come back, it doesn't insert a new record as the vehicle should already be reserved. Any help is appreciated!
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))


I'd assume obvious, but I did verify that there is in fact a record in the DB that it should return.

Answer

From the official docs:

If a forward cursor (the default) or dynamic cursor is used, FALSE is returned.

So you need to specify a different cursor, this is done like this:

$checkResults = sqlsrv_query(
    $conn, 
    $checkQuery, 
    array(), 
    array('Scrollable' => 'buffered'));