-1

I'm not really familiar with this type of stuff in PHP, however it isn't working too well. Before I share the code and what the issue is, let me tell you what my goal is. My goal is to have a ENUM selection in the database with values: '1','2','3' , 1 being a normal user, 2 being a moderator and 3 being administrator.

Here is the code:

$checkpermission = mysql_query("SELECT permission FROM users ");
$checkpermnrows = mysql_num_rows($checkpermission);

if ($checkpermnrows != 0) {
    if ($checkpermission == 2) {
        echo "User Group: Moderator";
    } else {
        echo "User Group: User";
    }
} else {
    echo "Rows not found";
}

if someone could also lecture me on how to setup sessions for this type of stuff, i'm pretty sure that would be better...

Barmar
  • 741,623
  • 53
  • 500
  • 612
deconv
  • 3
  • 2
  • 1
    It's not a good idea to use numeric values for a `ENUM`. The MySQL manual explains why. – Barmar Mar 31 '15 at 02:29
  • The usual point of using `ENUM` is to store the value as a small integer, but use a name for the input/output to the database. – Barmar Mar 31 '15 at 02:30
  • @Barmar what is a good alternative then? – deconv Mar 31 '15 at 02:30
  • Use an ENUM like `ENUM('normal', 'moderator', 'administrator')` – Barmar Mar 31 '15 at 02:31
  • Don't you need a `WHERE` clause in your `SELECT`, so you get the permissions of the current user? You're also missing the call to `mysql_fetch_XXX` to fetch a row of results. – Barmar Mar 31 '15 at 02:32
  • Maybe it's a good idea to create a class with constants to match enums present in database like here : http://stackoverflow.com/a/254528/4726998 In case you use these enums in more than one file... – fdglefevre Mar 31 '15 at 02:36
  • If that's what you're doing, what is your question? – Barmar Mar 31 '15 at 03:09
  • BTW, don't use the `mysql` extension, it's deprecated. Use `PDO` or `mysqli`, and use prepared statements. – Barmar Mar 31 '15 at 03:10
  • @barmar looks like I forgot to say what my problem was. woops, its kinda hard to post anything here when you get a insta -1 for asking a question. anyways, the verification isnt working. when i change the ENUM the echo still stays the same on the page. i dont think its properly working – deconv Mar 31 '15 at 03:15
  • You don't have a `WHERE` clause in your query, and you aren't calling a `fetch` function to get the permission value that you selected. How can your code possibly work? – Barmar Mar 31 '15 at 03:26
  • @Barmar exactly thats the problem i dont know what to do exactly for the enum. is it "SELECT permission FROM users WHERE permission=('1','2','3')"); or what? and then what would I put in mysql_fetch_assoc($query){ }? – deconv Mar 31 '15 at 03:47

1 Answers1

0

Assuming the logged in user ID is in $user, you would do:

$checkpermission = mysql_query("SELECT permission FROM users WHERE id = $user");
$row = mysql_fetch_assoc($checkpermission);
if ($row) {
    $permission = $row['permission'];
    if ($permission == 'moderator') {
        echo 'User Group: Moderator';
    } else {
        echo 'User Group: Users';
    }
} else {
    echo "Rows not found";
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • This is basic stuff you should have been able to get from any PHP+MySQL tutorial. I feel guilty posting it as an answer. – Barmar Mar 31 '15 at 04:04
  • what would be the difference between me going to a tutorial and learning the exact same thing than you just telling me it? – deconv Apr 03 '15 at 00:20
  • fair enough, well believe it or not I have learned a lot since asking this question and I feel quite dumb for asking it. nevertheless, thank you for your excellent answer. – deconv Apr 03 '15 at 01:53