I have this query that checks if a movement exists and supposed to return true or false. This query
function movement_performed_today($class_id, $client_id){
$class_id = (int)$class_id;
$client_id = (int)$client_id;
$query = mysql_query("SELECT COUNT(`movement`) FROM `completed_movements` WHERE `class_id` = '$class_id' AND `client_id` = '$client_id' AND `date` = CURDATE()");
$movement_performed = mysql_fetch_assoc($query);
$return = (mysql_result($movement_performed, 0) == 1) ? true : false;
var_dump ($return);
}
Returns:
bool(false)
bool(false)
If I replace this code:
$return = result(mysql_result($movement_performed, 0) == 1) ? true : false;
var_dump ($return);
With this:
print_r ($movement_perfomed);
Returns:
Array ( [COUNT(`movement`)] => 2 )
Array ( [COUNT(`movement`)] => 3 )
Am I completely wrong to think since these numbers are anything other than zero it should return true?
To answer your question:
mysql_resultreturnsFALSEon error. You are passing the array frommysql_fetch_associnstead of the mysql resource as required bymysql_result‘s function signature:Thus it returns
FALSEbecause it has an error.A better way:
This grabs the result of the count statement and does a
> 0check on it. The> 0check is not really needed but helps show intent rather than relying on on truthy values.Side note: the
mysql_*functions have been deprecated. You should migrate your code to useMySQLiorPDO. You are unfortunately using the fact that manymysql_*functions do not need themysqlresource. Migrating your code will be a pain because you have to change a bunch of functions or declare global variables. I recommend the former option but it will take a lot of effort to fix.