select from db using a variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phphlp47
Forum Newbie
Posts: 11
Joined: Mon Mar 08, 2010 6:49 pm

select from db using a variable

Post by phphlp47 »

Is it possible to do a select from a db table using a variable in the WHERE clause?

I have coded this but it does not return the field values in the php part of the form;

$rs_settings = mysql_query("select * from users WHERE id=@'saved_id'") or die(mysql_error());

I'm executing it with mypage.php?saved_id=2 ,
when I get this working I will be executing this from a page where the admin user will select the user then be presented
with just that users record.

I get the page displaying 2 in the record id but none of the other fields for id=2, I'm assuming its not actually reading the db.

Can somebody please advise the correct format to achieve this.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: select from db using a variable

Post by Christopher »

Do this:

Code: Select all

$saved_id = mysql_real_escape_string($_GET['saved_id'],  $link);
$rs_settings = mysql_query("select * from users WHERE id='$saved_id'",  $link);
if (!mysql_error($link)) {
     // display results
} else {
     // display error message
}
(#10850)
Post Reply