mySQL: problem searching for text string rather than number

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
User avatar
toro
Forum Newbie
Posts: 10
Joined: Wed Jul 03, 2002 8:03 am
Location: Iceland

mySQL: problem searching for text string rather than number

Post by toro »

Hello everyone,
When I search for an article based on a key (called $url_key because it will be passed as an URL parameter) like this:

Code: Select all

$query = "select * from articles where url_key = ".$url_key;
This works fine if the key is only a number, but as soon as there is a letter in the key (it becomes a string) this no longer works.

I've found a way to fix this using 'like' instead of '=':

Code: Select all

$query = "select * from articles where url_key like '%".$url_key."%'";
But being curious I was wondering why exactly this happens. Any pointers?

Thanks, Toro
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you have to quote strings with '
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

And you don't need to concatenate because you are within double quotes (")

$query = "select * from articles where url_key = '$url_key'";

will work fine
User avatar
toro
Forum Newbie
Posts: 10
Joined: Wed Jul 03, 2002 8:03 am
Location: Iceland

Ah - I can see clearly now...

Post by toro »

Thanks a lot guys. :)

Toro
Post Reply