We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,153 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Why does this function always return true?

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;
}
4
Contributors
3
Replies
50 Minutes
Discussion Span
3 Months Ago
Last Updated
4
Views
Noth
Newbie Poster
7 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

pritaeas
Posting Prodigy
Moderator
9,293 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,462
Skill Endorsements: 86

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.

diafol
Keep Smiling
Moderator
10,662 posts since Oct 2006
Reputation Points: 1,628
Solved Threads: 1,513
Skill Endorsements: 57

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..

veedeoo
Master Poster
735 posts since Oct 2011
Reputation Points: 298
Solved Threads: 130
Skill Endorsements: 13

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0725 seconds using 2.94MB