query not working if WHERE variable not number

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
chopWood
Forum Commoner
Posts: 45
Joined: Fri Apr 30, 2010 9:28 am

query not working if WHERE variable not number

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: query not working if WHERE variable not number

Post by AbraCadaver »

Try:

Code: Select all

$q = "SELECT antiques_id FROM antiques WHERE item_num = '$id' LIMIT 1";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
chopWood
Forum Commoner
Posts: 45
Joined: Fri Apr 30, 2010 9:28 am

Re: query not working if WHERE variable not number

Post by chopWood »

Yes! Thank you for your suggestion.

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

thanks again,

Chop
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: query not working if WHERE variable not number

Post by Benjamin »

chopWood wrote:Apparently the single quotes treats it as a string rather than a number?
Close, strings must be enclosed in quotes.
Post Reply