Stumped on using numeric key

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
jimandy
Forum Newbie
Posts: 22
Joined: Sat Sep 15, 2007 10:56 am
Location: Alabama, USA

Stumped on using numeric key

Post by jimandy »

I made a simple table for storing member names with a numeric key ...
CREATE TABLE `people` (
`mbrID` tinyint(3) NOT NULL,
`name` varchar(20) NOT NULL
PRIMARY KEY (`mbrID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
and populated it with this data...
INSERT INTO `people` VALUES (21, 'John Smith');
I want to retrieve the record in here from a variable passed to this simple script...

Code: Select all

<?$db_name ="MRM";
include('connect.php');   //(connection details)
$mbrNbr = 21;               // I want to look up  the record for this value

$sql = 'SELECT * FROM `people` WHERE `mbrID` = "21";';
$result = @mysql_query($sql,$connection)or die(mysql_error());

// view the result
$row=mysql_fetch_array($result);
foreach($row as $item){print $item;}
?>
The above works but if I substitute "$mbrNbr" in place of "21", in the query it does not work.
Since the value in the $mbrNbr variable is same as the literal numeric it seems it should work.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Stumped on using numeric key

Post by Celauran »

Variables aren't evaluated inside single quotes.

This should work:

Code: Select all

$sql = "SELECT * FROM `people` WHERE `mbrID` = {$mbrNbr}";
$result = @mysql_query($sql,$connection)or die(mysql_error());
jimandy
Forum Newbie
Posts: 22
Joined: Sat Sep 15, 2007 10:56 am
Location: Alabama, USA

Re: Stumped on using numeric key

Post by jimandy »

deed it does, celauren - thanks.

And after hours of trying I realized I should print my query out and see what was going on which led me to also get this to work...

Code: Select all

$sql = "SELECT * FROM `people` WHERE `mbrID` =".$mbrNbr." LIMIT 0, 30";
Two ways to skin same cat!
Post Reply