Saturday 12 January 2019

mysql - 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!
';

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


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.



Answer



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.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...