1.11M Members

Why does this function always return true?

 
0
 

Hi guys.

I'm just wondering why this function always returns true, no matter what the $user_id is. The $user_id variable is determined by what is retreived from the $_SESSION.

I am well aware that mysql_* functions are being depreceated etc, no need to tell me :P

Thanks guys!

function user_mod() {
    $user_id = $_SESSION['user_id'];
    return (mysql_result(mysql_query("SELECT COUNT($user_id) FROM `users` WHERE `permissions` = 1 OR `permissions` = 2"), 0) == 1) ? true : false;
}
 
0
 

The query is off. What are you trying to achieve?

 
0
 

Try running this and you'll find out why:

SELECT COUNT(3) FROM `users`

This is what you're basically doing - you'll always get a single row - usually with '0' as the value.

 
0
 

Hi,

it will always return true, because of your where clause

WHERE `permissions` = 1 OR `permissions` = 2"

if there is nothing to count in user_id, then your codes above will return how many members with permission 1 and permission 2. Thus, your script above will literally return the entire count for perm 1 and perm 2.

You can add AND matching only the user_id,, but I am not sure if this is what you are trying to do..

Something like this..

("SELECT COUNT($user_id) FROM `users` WHERE user_id = '".$user_id."' AND `permissions` = 1 OR `permissions` = 2"), 0)

That should return either 0 or the total count of members with the same user ID and with either permission 1 or permission 2..

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: