Russia Posted March 13, 2011 Share Posted March 13, 2011 hey gys im trying to build a permissions system based on the users group. Groups can be 1, 2, or 3. Here is the code im using: <?php mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("chat") or die(mysql_error()); $result = mysql_query("SELECT * FROM `members` WHERE username='".$_SESSION['username']."'") or die(mysql_error()); if($result["group"]==1){ echo 'yes'; } else { echo 'no'; } ?> And here is how my db looks like: The thing is, when Im logged in with the user test and run that script, its saying no as the group im in isnt 1. Why is this happening? Can someone help me fix my code. Quote Link to comment https://forums.phpfreaks.com/topic/230541-check-if-value-in-row-is-equal-to-value-1/ Share on other sites More sharing options...
sasa Posted March 13, 2011 Share Posted March 13, 2011 you must fetch result $row = mysql_fetch_array($result); and after test against $row['group'] if($result["group"]==1){ Quote Link to comment https://forums.phpfreaks.com/topic/230541-check-if-value-in-row-is-equal-to-value-1/#findComment-1187115 Share on other sites More sharing options...
Russia Posted March 13, 2011 Author Share Posted March 13, 2011 Okay thanks. also, now can i do another else if the person is group 3? like 1,2,3? Quote Link to comment https://forums.phpfreaks.com/topic/230541-check-if-value-in-row-is-equal-to-value-1/#findComment-1187116 Share on other sites More sharing options...
sasa Posted March 13, 2011 Share Posted March 13, 2011 shure you can Quote Link to comment https://forums.phpfreaks.com/topic/230541-check-if-value-in-row-is-equal-to-value-1/#findComment-1187121 Share on other sites More sharing options...
Russia Posted March 13, 2011 Author Share Posted March 13, 2011 Like this? <?php mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("chat") or die(mysql_error()); $q = mysql_query("SELECT * FROM `members` WHERE username='".$_SESSION['username']."'") or die(mysql_error()); $result = mysql_fetch_array($q); if($result["group"]==1){ echo 'yes';//regular user } elseif($result['group'] == 2) { echo 'maybe';//mod } elseif($result['group'] == 3) { echo 'for sure'; //admin } else { echo 'no';//other type of user like banned, unactivated } ?> Quote Link to comment https://forums.phpfreaks.com/topic/230541-check-if-value-in-row-is-equal-to-value-1/#findComment-1187122 Share on other sites More sharing options...
Genesis730 Posted March 13, 2011 Share Posted March 13, 2011 Yes, you can do an if statement for each case if ($result['group'] == 1) { echo 'Group 1'; } elseif ($result['group'] == 2) { echo 'Group 2'; } else { echo 'Group 3'; or do a switch (much easier if you have multiple groups and it looks nicer switch ($result['group']) { case "1": echo "Group 1"; break; case "2": echo "Group 2"; break; case "3": echo "Group 3"; break; default: echo "Error, Group does not exist; break; } Quote Link to comment https://forums.phpfreaks.com/topic/230541-check-if-value-in-row-is-equal-to-value-1/#findComment-1187123 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.