klepec Posted April 15, 2012 Share Posted April 15, 2012 I have a search form. $req is a keyword or an ID number input by user. As you can see, query checks only for rows where userID matches the current login userID. My question is, how to transform (not independent query for admins) the query to search all rows if user $access is admin (no matter the administrator's own userID). <?php $userID="something"; (from session) $access="something" (from session / user or admin) $query="SELECT esName, esID, esAddress FROM estates WHERE (esName LIKE '$req' OR esID LIKE '$req') AND userID='$userID'"; mysql_query($query); ?> Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/260980-admin-access-mysql-query/ Share on other sites More sharing options...
MMDE Posted April 15, 2012 Share Posted April 15, 2012 I think you will have to explain this a bit more in detail? The solution I see to your question is far too obvious for it to be the solution... if($access=='admin'){ } o.O Quote Link to comment https://forums.phpfreaks.com/topic/260980-admin-access-mysql-query/#findComment-1337532 Share on other sites More sharing options...
klepec Posted April 15, 2012 Author Share Posted April 15, 2012 I think you will have to explain this a bit more in detail? The solution I see to your question is far too obvious for it to be the solution... if($access=='admin'){ } o.O Hey, Like i said i would like that condition inside mysql query, not writing another query for admins using php if statement. The problem here is that i have like 10 different queries for 10 different databases and writing another admin query for each one would be a big mess. Quote Link to comment https://forums.phpfreaks.com/topic/260980-admin-access-mysql-query/#findComment-1337533 Share on other sites More sharing options...
PFMaBiSmAd Posted April 15, 2012 Share Posted April 15, 2012 <?php $access = 'admin'; $admin = $access == 'admin' ? ' OR 1' : ''; $query="SELECT esName, esID, esAddress FROM estates WHERE (esName LIKE '$req' OR esID LIKE '$req') AND userID='$userID'$admin"; echo $query; Quote Link to comment https://forums.phpfreaks.com/topic/260980-admin-access-mysql-query/#findComment-1337537 Share on other sites More sharing options...
klepec Posted April 15, 2012 Author Share Posted April 15, 2012 <?php $access = 'admin'; $admin = $access == 'admin' ? ' OR 1' : ''; $query="SELECT esName, esID, esAddress FROM estates WHERE (esName LIKE '$req' OR esID LIKE '$req') AND userID='$userID'$admin"; echo $query; This looks good Can i ask what "OR 1" does? Quote Link to comment https://forums.phpfreaks.com/topic/260980-admin-access-mysql-query/#findComment-1337567 Share on other sites More sharing options...
klepec Posted April 15, 2012 Author Share Posted April 15, 2012 ok nevermind figured it out. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/260980-admin-access-mysql-query/#findComment-1337576 Share on other sites More sharing options...
PFMaBiSmAd Posted April 15, 2012 Share Posted April 15, 2012 Anything logically OR'ed with a true (1) value will be true. Therefor, userID='$userID' can be true or false and the result will be true. Quote Link to comment https://forums.phpfreaks.com/topic/260980-admin-access-mysql-query/#findComment-1337580 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.