Page 1 of 1

Getting user_id using information from another column

Posted: Mon Oct 22, 2012 11:21 am
by JuiceYum
Hey guys,

I've been sitting her for hours trying to figure this out. I've Google'd like crazy as well but can't find an answer to my problem and that is that I can't get the information from another column using information from another column. This is how my table is set up.

(Ignore the - as they are to fill in spaces.

Table name: users

user_id | user_email --------|user_pass
-----1----|--false@fake.com--|-------fake--------

Now, I get the email from the session and I want to get the user_id from the email. Here is what I have right now but I just can't get it to work.

(Please note that $email is defined as below and the session is working correctly as I can output the email.)[syntax=php]$email = $_SESSION['user_email']; [/syntax]

[syntax=php]$res = mysql_query("SELECT `user_id` FROM `users` WHERE '{$email}' = `user_id`");
$user = mysql_fetch_assoc($res);
echo $user;[/syntax]

All help would be greatly appreciated!

Re: Getting user_id using information from another column

Posted: Mon Oct 22, 2012 11:33 am
by Temor
Well, this won't work, for obvious reasons.
[syntax=php]$res = mysql_query("SELECT `user_id` FROM `users` WHERE '{$email}' = `user_id`");[/syntax]

user_id is not equal to the email address. user_id is an integer, user_email is an email.

This should work.
[syntax=php]$res = mysql_query("SELECT `user_id` FROM `users` WHERE `user_email` = '{$email}'");[/syntax]

Re: Getting user_id using information from another column

Posted: Mon Oct 22, 2012 9:12 pm
by JuiceYum
Thanks but when I put that in and echo $res it just says Array but if I echo $user I get Resource error #5. I'm baffled.

Re: Getting user_id using information from another column

Posted: Mon Oct 22, 2012 9:58 pm
by Temor
Well, you can't just echo it straight away, and you should use mysql_result instead since you're only fetching one value.
[syntax=php]
$res = mysql_query("SELECT `user_id` FROM `users` WHERE `user_email` = '{$email}'");

$id = mysql_result($res);

[/syntax]

echoing $id should give you the id.