Getting user_id using information from another column

Ask about a PHP problem here.
Post Reply
JuiceYum
Posts: 18
Joined: Fri Jan 27, 2012 8:11 am

Getting user_id using information from another column

Post 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.)
$email = $_SESSION['user_email']; 
$res = mysql_query("SELECT `user_id` FROM `users` WHERE '{$email}' = `user_id`");
				$user = mysql_fetch_assoc($res);
				echo $user;
All help would be greatly appreciated!
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Getting user_id using information from another column

Post by Temor »

Well, this won't work, for obvious reasons.
$res = mysql_query("SELECT `user_id` FROM `users` WHERE '{$email}' = `user_id`");
user_id is not equal to the email address. user_id is an integer, user_email is an email.

This should work.
$res = mysql_query("SELECT `user_id` FROM `users` WHERE `user_email` = '{$email}'");
JuiceYum
Posts: 18
Joined: Fri Jan 27, 2012 8:11 am

Re: Getting user_id using information from another column

Post 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.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Getting user_id using information from another column

Post 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.
$res = mysql_query("SELECT `user_id` FROM `users` WHERE `user_email` = '{$email}'");

$id = mysql_result($res);

echoing $id should give you the id.
Post Reply