Jump to content

check if value in row is equal to value '1'


Russia

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;

}

Archived

This topic is now archived and is closed to further replies.

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