Recur
Recur

Reputation: 1428

php mysql_fetch_assoc isn't working as expected

Hello I have the following php code here:

    $username = trim(mysql_real_escape_string($username));
    $password = md5(trim(mysql_real_escape_string($password)));
    $query = "SELECT user_level FROM user WHERE username='$username' AND password='$password'";
    $result = mysql_query($query) or die('query did not go through');
    if($result!=false){
        echo 'result is not false!<br/>';
        while($row = mysql_fetch_assoc($result)){
        echo 'another output to test'.$row['user_level'].'<br/>';
        }
    }

I know the database entry exists.. its supposed to be '1' but for some reason it is not outputting 1. Am I missing something really small here that I just can't catch at this moment?

edit: ok I've changed username to user_level but it is not getting anything. The echo in the while loop isn't even outputting anything.

edit2: I also forgot to mention when I do output the $result it says resource id #5 so I know its getting something from the query.

edit 3: (2/10/2012):Just to let you guys know, it was a mistake to get the username, I meant to get user_level from the user in the user table.

the output for the query is: the query is this value SELECT user_level FROM user WHERE username='spankerer' AND password='246b8ae01899a116369ddcba84f425fd'

print_r($row); is not being displayed in the while loop.

edit 4: forgot to mention if I do the mysql query from the cmd, I get the table of user_level displaying 1 so it is correct.

Upvotes: 1

Views: 4971

Answers (4)

Recur
Recur

Reputation: 1428

So it seems to have been my database issue...

I set the maximum characters for password to be 20 and when the user put in the password, PHP would hash it and try to match the 32 character key with the database's 20 character key.

Upvotes: 0

Chuck Burgess
Chuck Burgess

Reputation: 11574

Your query should only query for user_level:

$query = "SELECT user_level FROM user WHERE username='$username' AND password='$password'";

EDIT:

Will it work if you change your while loop from:

while($row = mysql_fetch_assoc($result)){
    echo 'another output to test'.$row['user_level'].'<br/>';
}

to

while($row = mysql_fetch_array($result)){
    echo 'another output to test'.$row['user_level'].'<br/>';
}

Upvotes: 1

Astha
Astha

Reputation: 1734

Your select query should be like

$query = "SELECT user_level, username FROM user WHERE username='$username' AND password='$password'";

EDIT

echo your query also as:

 $query = "SELECT user_level, username FROM user WHERE username='$username' AND password='$password'";
echo $query;

Run the same query in database and see if it gives you what? Also in while loop firstly do

 while($row = mysql_fetch_assoc($result)){
   print_r($row);
    }

and see is the output same as u get from direct query execution.

Upvotes: 4

Rukmi Patel
Rukmi Patel

Reputation: 2561

In select argument you need to fetch column called "user_level".It is missing in your query.

Upvotes: 1

Related Questions