richiec Posted October 25, 2007 Share Posted October 25, 2007 Hey people, what i am trying to do is just make a very simple user access level. Ill explain as much as i can for you to understand. In mysql db i have a simple table for users, with 4 fields; 1) id 2) username 3) password 4) access My login system works fine that isnt a problem, now in the access field what i am trying to do is in there either have "admin" or "member" depending on the user. The reason behind this is so in the php members area it echos different things depending on that users access (admin or member in the access field) something like this... <? $nick = $_SESSION["myusername2"]; $query="SELECT * FROM `user` WHERE 'username' = $nick"; $result=mysql_query($query) or die(hmm); $num = mysql_num_rows($result); if (mysql_result($result,$i,"access") == 'admin'){ echo "<admin user page>"; } elseif (mysql_result($result,$i,"access") == 'members'){ echo "<members user page>"; } ?> any ideas? Thanks Quote Link to comment Share on other sites More sharing options...
premiso Posted October 25, 2007 Share Posted October 25, 2007 <?php $nick = $_SESSION["myusername2"]; $query="SELECT access FROM `user` WHERE 'username' = $nick"; $result=mysql_query($query) or die(hmm); $num = mysql_num_rows($result); $isAdmin = ($result['access'] == "admin")?true:false; if ($isAdmin) { echo "<admin user page>"; } else{ echo "<members user page>"; } ?> A note <? has been depreciated, use <?php That is a basic usage given there are only 2 roles, admin and member. Then anytime you need to display admin feature you check isAdmin. Quote Link to comment Share on other sites More sharing options...
chocopi Posted October 25, 2007 Share Posted October 25, 2007 you could use switch <?php $nick = $_SESSION["myusername2"]; $query="SELECT access FROM `user` WHERE 'username' = $nick"; $result=mysql_query($query) or die(hmm); $num = mysql_num_rows($result); switch($result['access']) { case admin: // admin page break; case mod: // mod page break; case normal: // normal page break; } ?> if you get rid of the break's then the admin would see admin,mod,normal and mod would see mod,normal and normal would just see normal. If that makes sense replace admin, mod and normal with whatever values you are expecting. Hope that helps ~ Chocopi Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted October 25, 2007 Share Posted October 25, 2007 A note <? has been depreciated, use <?php what!?!?!?!? Quote Link to comment Share on other sites More sharing options...
richiec Posted October 25, 2007 Author Share Posted October 25, 2007 i tried both of those, and both return with the "hmm" error from $query any idea why? Thanks though and the switch seems like a good idea if i can get it to work. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted October 25, 2007 Share Posted October 25, 2007 use or die(mysql_error()); the problem is probably your use of single-quotes around the field name... and potentially a problem with lack of single-quotes around $nick. Quote Link to comment Share on other sites More sharing options...
premiso Posted October 25, 2007 Share Posted October 25, 2007 A note <? has been depreciated, use <?php what!?!?!?!? I guess to say they have been depreciated would be incorrect, there has been talk on php.net stating that it may be in the future so code for it: Short open tags, register globals, and magic quotes (perhaps others) were lazy-way short cuts to get the language to do things that the programmer should have been doing himself (typing a few characters, referencing the proper variable array, and escaping special characters in only the cases where they needed to be escaped.) All three of these have resulted in more long term work to troubleshoot and fix then what they saved in time in the short term. Aside from the disadvantages introduced by these three things, they also unfortunately introduced multiple ways of accomplishing the same thing that are now dependent on server settings to work. Basically <?php is the proper cross-server way to code as it will work on any php server, the short tags may not work due to server settings etc. Quote Link to comment Share on other sites More sharing options...
premiso Posted October 25, 2007 Share Posted October 25, 2007 $query="SELECT access FROM `user` WHERE `username` = '$nick'"; Single quote around values, backticks around tables and columns. Quote Link to comment Share on other sites More sharing options...
richiec Posted October 25, 2007 Author Share Posted October 25, 2007 Now i just get a blank page but atleast it gives me no errors which probably just means its a html layout error or something so i should be able to figure it out. Thanks alot for all your help! Enjoy the rest of your day/night Rich. Quote Link to comment Share on other sites More sharing options...
premiso Posted October 25, 2007 Share Posted October 25, 2007 Now i just get a blank page but atleast it gives me no errors which probably just means its a html layout error or something so i should be able to figure it out. Thanks alot for all your help! Enjoy the rest of your day/night Rich. Remove the < and > from the output or view the source. Remember that < and > are considered html tags and will be hidden in the source of a page and not displayed. Quote Link to comment Share on other sites More sharing options...
richiec Posted October 25, 2007 Author Share Posted October 25, 2007 Sorry to reopen this, but i still cant get it, ive changed it to the if/else command insted of the switch command but still cant get it. I have just set it to do something simple once i get it to work i can put it all back into place with the right layouts for admin/members ect this is what i have.. $nick = $_SESSION["myusername2"]; $query="SELECT `access` FROM `user` WHERE `username` = '$nick'"; $result=mysql_query($query) or die(mysql_error()); $num = mysql_num_rows($result); $isAdmin = ($result['access'] == "admin"); if ($isAdmin) echo "admin test"; else echo "members test"; Now i have loged in as someone with admin and someone with member as there access but on both it echos "members test" it doesnt echo "admin test" on the admin user. Any ideas? Quote Link to comment Share on other sites More sharing options...
per1os Posted October 26, 2007 Share Posted October 26, 2007 Your missing the point of an earlier post by premiso. $isAdmin = ($result['access'] == "admin")?true:false; That uses what is called a ternary operator. the ? and : which is basically a shortened if/else. I suggest you change: $isAdmin = ($result['access'] == "admin"); TO $isAdmin = ($result['access'] == "admin")?true:false; and give it a try. Quote Link to comment Share on other sites More sharing options...
richiec Posted October 26, 2007 Author Share Posted October 26, 2007 Still not working, it still just displays "members test" when i login to an account which should say "admin test" Any more ideas? ??? Quote Link to comment Share on other sites More sharing options...
premiso Posted October 26, 2007 Share Posted October 26, 2007 $nick = $_SESSION["myusername2"]; $query="SELECT `access` FROM `user` WHERE `username` = '$nick'"; $result=mysql_query($query)) or die(mysql_error(); $num = mysql_num_rows($result); $result = mysql_fetch_array($result); $isAdmin = ($result['access'] == "admin"); if ($isAdmin) echo "admin test"; else echo "members test"; Forgot to fetch the array of the query result, try that. Quote Link to comment Share on other sites More sharing options...
richiec Posted October 26, 2007 Author Share Posted October 26, 2007 Same thing :'( Quote Link to comment Share on other sites More sharing options...
premiso Posted October 26, 2007 Share Posted October 26, 2007 <?php $nick = $_SESSION["myusername2"]; echo "Username is: " . $nick; $query="SELECT `access` FROM `user` WHERE `username` = '$nick'"; $result=mysql_query($query)) or die(mysql_error()); // error here fixed $num = mysql_num_rows($result); $result = mysql_fetch_array($result); print_r($result); $isAdmin = ($result['access'] == "admin"); if ($isAdmin) echo "admin test"; else echo "members test"; ?> Try some debugging procedures, make sure what is expected to check against is being checked and that you are getting a result. Also check the case on admin as php is CaSeSenSitIvE. Quote Link to comment Share on other sites More sharing options...
richiec Posted October 26, 2007 Author Share Posted October 26, 2007 YES!!! that worked mate, thank you very much, all of you who helped me here! Enjoy the rest of your day/night much love :x Rich. Quote Link to comment 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.