[PHP HELP] User Rank

bodge

ayy lmao
Oct 31, 2011
406
54
Hello Devbest, I am trying to code a rank system like Habbo for example if a user rank is 3 then it shows links. Here is what I've done, but it doesn't work :S

Rank code
Code:
$rank = mysql_query("SELECT rank FROM users WHERE id = '".$_SESSION['id']."'") or die(mysql_error());

PHP code

Code:
<?php if ($rank > 3) echo "lol"; ?>

Users which have a rank id of 1 and 2 can see the echoed "lol" which I don't want them to see, I only want that for users with rank 3.
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,638
2,393
I think you'd have to do:

PHP:
<?php
$rank = mysql_result(mysql_query("SELECT `rank` FROM `users` WHERE `id` = '" . $_SESSION['id'] . "'"), 0) or die(mysql_error());
if ($rank > 3) {
    echo 'lol';
}
?>

Another thing you should do is sanitize your $_SESSION variables, you can do this in a global file and then include it into each PHP file where you will be using a database query. This is because I'm sure users can change $_SESSION data and change it to be able to do SQL injections. Either that or look into and specifically check out prepared statements.

I usually create a class aimed at users and create a method called getRow or something which accepts 2 parameters, the user ID and the row to fetch a value from, the method will then return the value of the row, if any.
 

bodge

ayy lmao
Oct 31, 2011
406
54
I think you'd have to do:

PHP:
<?php
$rank = mysql_result(mysql_query("SELECT `rank` FROM `users` WHERE `id` = '" . $_SESSION['id'] . "'"), 0) or die(mysql_error());
if ($rank > 3) {
    echo 'lol';
}
?>

Another thing you should do is sanitize your $_SESSION variables, you can do this in a global file and then include it into each PHP file where you will be using a database query. This is because I'm sure users can change $_SESSION data and change it to be able to do SQL injections. Either that or look into and specifically check out prepared statements.

I usually create a class aimed at users and create a method called getRow or something which accepts 2 parameters, the user ID and the row to fetch a value from, the method will then return the value of the row, if any.
This just showed up blank, but thanks anyway.
If it helps here's my stricture for `ranks`

Code:
ALTER TABLE  `users` CHANGE  `rank`  `rank` INT( 11 ) UNSIGNED NOT NULL DEFAULT  '1'
 

Ecko

23:37 [autobots] -!- eckostylez [[email protected]]
Nov 25, 2012
1,398
962
I just got
Code:
Resource id #4

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error

PHP:
$row = mysql_fetch_assoc(mysql_query("SELECT `rank` FROM `users` WHERE `id` = '" . $_SESSION['id'] . "'"), 0) or die(mysql_error());
echo $row['rank'];
 

Users who are viewing this thread

Top