Why is this returning bool(false)?

40,332

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.

Share:
40,332
Colbyd
Author by

Colbyd

Updated on July 09, 2022

Comments

  • Colbyd
    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
    NullUserException almost 12 years
    mysqli_stmt_num_rows() (replacement for mysql_num_rows()) exists for a reason.
  • Colbyd
    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
    Colbyd almost 12 years
    Now when I do a var_dump ($return) it get bool(true) bool(true) which is correct since movement does exist from this user in this class on this day. When I do var_dump (isPerformed) from the while loop it returns NULL and therefore executing the function which I dont want it do if returns true.
  • Colbyd
    Colbyd almost 12 years
    can you tell me what this line does $return = $movement_performed[0] > 0;
  • Levi Morrison
    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
    Colbyd almost 12 years
    my code currently looks like this $return = $movement_performed[0] > 0;
  • Levi Morrison
    Levi Morrison almost 12 years
    @Colbyd Change it to return $movement_performed[0] > 0; and see if isPerformed is still NULL.
  • Colbyd
    Colbyd almost 12 years
    that worked. Thank you...you'll have to forgive my ignorance on this. Im pretty green incase you couldnt tell.
  • Colbyd
    Colbyd almost 12 years
    now it returns bool(false) not matter if movement exist or not.