Why is this returning bool(false)?
To answer your question:
mysql_result
returns FALSE
on error. You are passing the array from mysql_fetch_assoc
instead of the mysql resource as required by mysql_result
's function signature:
string mysql_result ( resource $result , int $row [, mixed $field = 0 ] )
Thus it returns FALSE
because it has an error.
A better way:
// Change
$movement_performed = mysql_fetch_assoc($query);
$return = result(mysql_result($movement_performed, 0) == 1) ? true : false;
// to
$movement_performed = mysql_fetch_row($query);
return $movement_performed[0] > 0;
This grabs the result of the count statement and does a > 0
check on it. The > 0
check 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 use MySQLi
or PDO
. You are unfortunately using the fact that many mysql_*
functions do not need the mysql
resource. 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.
Colbyd
Updated on July 09, 2022Comments
-
Colbyd almost 2 years
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?
-
NullUserException almost 12 years
mysqli_stmt_num_rows()
(replacement formysql_num_rows()
) exists for a reason. -
Colbyd almost 12 years@LeviMorrison thanks for the help that worked. I posted the php loop above where Im calling this function and what Im trying to accomplish. Can you take a look and tell me what Im doing wrong when calling this functioin?
-
Colbyd almost 12 yearsNow when I do a
var_dump ($return)
it getbool(true) bool(true)
which is correct sincemovement
does exist from this user in this class on this day. When I dovar_dump (isPerformed)
from the while loop it returns NULL and therefore executing the function which I dont want it do if returns true. -
Colbyd almost 12 yearscan you tell me what this line does
$return = $movement_performed[0] > 0;
-
Levi Morrison almost 12 years@Colbyd Did you actually say
return $return;
in your function? You have to return the result from the function. I edited my code example to reflect this. -
Colbyd almost 12 yearsmy code currently looks like this
$return = $movement_performed[0] > 0;
-
Levi Morrison almost 12 years@Colbyd Change it to
return $movement_performed[0] > 0;
and see ifisPerformed
is stillNULL
. -
Colbyd almost 12 yearsthat worked. Thank you...you'll have to forgive my ignorance on this. Im pretty green incase you couldnt tell.
-
Colbyd almost 12 yearsnow it returns bool(false) not matter if
movement
exist or not.