just_user
just_user

Reputation: 12059

mysql_fetch_assoc() gives me the wrong value

When I make the query bellow I can do a password check and the password and it works. How ever, when I try to extract the row id called id it returns 0 when it should be something else. And when I print the password the same way it prints two digits instead of the string I was hoping for.

I more or less a beginner to PHP but maybe someone could see whats wrong with my code?

$sql = 'SELECT pass FROM tbl_user WHERE email = "'.$email.'" LIMIT 1 ;';
$selection = mysql_query($sql);

$r = mysql_fetch_assoc($selection);
$user_id = $r['id'];
print "user id is: " + $r['id'];
print "user id is: " + $r['pass'];

Upvotes: 1

Views: 158

Answers (3)

wanovak
wanovak

Reputation: 6127

You're only selecting pass in your query; so the id isn't returned.

Upvotes: 3

konsolenfreddy
konsolenfreddy

Reputation: 9671

You only select the pass, not the id

Use this query:

$sql = 'SELECT id, pass FROM tbl_user WHERE email = "'.$email.'" LIMIT 1 ;';

Upvotes: 3

Joe
Joe

Reputation: 15802

You're only SELECTing the password. Change your query to

$sql = 'SELECT id, pass FROM tbl_user WHERE email = "'.$email.'" LIMIT 1 ;';

With regards to printing it, instead of using print, use echo. Also, instead of +, use . to join strings together:

echo "user id is: " . $r['id'];
echo "user id is: " . $r['pass'];

Upvotes: 5

Related Questions