Jump to content

Recommended Posts

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:

 

zVcMf.png

 

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

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
}
?> 

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;

}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.