[PHP] Can't find error

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
For some reason PHP is throwing this error at me:
Notice
: Undefined variable: rank in
C:\xampp\htdocs\main\user_bar.html
on line
18

Here is my code (I hope you can find the error because I sure as hell can't!):

PHP:
$q = mysql_query("SELECT * FROM users WHERE username = '$username'");
while($row = mysql_fetch_assoc($q)){
$rank = $row['rank'];
echo $rank;
}
?>
<div id="top_bar">
Welcome back, <?php echo $_SESSION['myusername'] . "  ";?><br />
| <a href="logout.php">Logout</a>
<?php
if($rank == "admin"){
echo " | <a href=\"add_post.php\">Create Blog Post</a> | ";
}else{
 
}
?>
<a href="messages.php">Messages</a> |
</div>
 

tyr0ne

Active Member
Mar 29, 2012
141
14
try to echo your $row variable first. can always use print_r for more info (echo will only ouput Array())

from there well it depends on what is printed.
 

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
Neither echo or print_r produces any result while used inside the while.

----------- EDIT -----------

So I logged out of the account with user status, logged into an account with admin status then re-logged into the account with user status and it fixed it.

Would it have been something to do with my session maybe?
 

IntactDev

Member
Nov 22, 2012
399
71
Neither echo or print_r produces any result while used inside the while.

----------- EDIT -----------

So I logged out of the account with user status, logged into an account with admin status then re-logged into the account with user status and it fixed it.

Would it have been something to do with my session maybe?
That, or $rank wasn't defined anywhere.
 

Weasel

👄 I'd intercept me
Nov 25, 2011
4,132
2,456
I think your errors are on developer mode. You can use this to prevent errors like that:
PHP:
error_reporting(E_ALL ^ E_NOTICE);

Or add:
PHP:
$rank = '';

This errors are saying that the $rank var isn't set, untill the rows are requested from the database (so aren't set). This isn't anything that you should prevent because its a security risk, but some coders like to do stuff like that.
 

Users who are viewing this thread

Top