Page 1 of 1

mySQL: problem searching for text string rather than number

Posted: Tue Jul 30, 2002 7:01 pm
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

Posted: Tue Jul 30, 2002 10:16 pm
by volka
you have to quote strings with '

Posted: Wed Jul 31, 2002 4:37 am
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

Ah - I can see clearly now...

Posted: Wed Jul 31, 2002 7:29 am
by toro
Thanks a lot guys. :)

Toro