Page 1 of 1

query not working if WHERE variable not number

Posted: Tue May 18, 2010 10:33 am
by chopWood
The following query does not work if, let's say, $id = "2341f43". It works okay if $id only consists of numbers:

Code: Select all

$q = "SELECT antiques_id FROM antiques WHERE item_num = $id LIMIT 1";
		$r = @mysqli_query ($dbc, $q); // Run the query.
		if ($r) {
				$row = @mysqli_fetch_array ($r, MYSQLI_ASSOC);
				echo $row['antiques_id'];
		}
		else {
		echo ' querry did not work';
		} 
If it makes any difference, the 'item_num' column in the table is assigned varchar(20)

thank you for your assistance,
chop

Re: query not working if WHERE variable not number

Posted: Tue May 18, 2010 10:37 am
by AbraCadaver
Try:

Code: Select all

$q = "SELECT antiques_id FROM antiques WHERE item_num = '$id' LIMIT 1";

Re: query not working if WHERE variable not number

Posted: Wed May 19, 2010 7:39 am
by chopWood
Yes! Thank you for your suggestion.

Apparently the single quotes treats it as a string rather than a number?

thanks again,

Chop

Re: query not working if WHERE variable not number

Posted: Wed May 19, 2010 7:40 am
by Benjamin
chopWood wrote:Apparently the single quotes treats it as a string rather than a number?
Close, strings must be enclosed in quotes.