1,105,456 Community Members

Why does this function always return true?

Member Avatar
Noth
Newbie Poster
7 posts since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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;
}
Member Avatar
pritaeas
mod_pritaeas
11,310 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,835 [?]
Skill Endorsements: 155 [?]
Moderator
Featured
Sponsor
 
0
 

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

Member Avatar
diafol
Where are my eyes?
12,970 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,847 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
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.

Member Avatar
veedeoo
Veteran Poster
1,014 posts since Oct 2011
Reputation Points: 314 [?]
Q&As Helped to Solve: 166 [?]
Skill Endorsements: 17 [?]
 
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 three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: