Page 1 of 1

Stumped on using numeric key

Posted: Tue Feb 07, 2012 3:06 pm
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.

Re: Stumped on using numeric key

Posted: Tue Feb 07, 2012 3:54 pm
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());

Re: Stumped on using numeric key

Posted: Tue Feb 07, 2012 4:39 pm
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!