Click to See Complete Forum and Search --> : problems using LIKE % $x %


mameha1977
10-19-2006, 03:51 AM
Im having trouble with the LIKE operator.

I have some 'country' strings like this:

1) "AR AU"
2) "AR"
3) "AU"

I want to get strings that contain AR, ie 1 and 2. But when I do the following query, it returns 1, 2 and 3, because the A in AU seems to be enough to satisfy the LIKE:

$country = AR;

SELECT * FROM table WHERE country LIKE "%'.$country.'%"

How do I make it only return strings that contain exactly AR?

chazzy
10-19-2006, 06:56 AM
that's not even a valid query, that's why you're getting bad results. are you using mysql?


//this is assuming you use PHP...
$country = "AR";//note that it must be enclosed in quotes
$sql = "SELECT * FROM table WHERE country like '%".$country."%'";

russell
10-19-2006, 10:48 AM
to make it return only Exactly "AR"

//this is assuming you use PHP...
$country = "AR";//note that it must be enclosed in quotes
$sql = "SELECT * FROM table WHERE country = '".$country."'";

mameha1977
10-19-2006, 07:22 PM
No, I want it also to return the "AR AU" string. Basically if AR occurs anywhere within a string I want it returned.

chazzy
10-19-2006, 09:16 PM
then read my post, it is correct.

mameha1977
10-19-2006, 09:46 PM
sorry my original post was badly laid out.

in fact i am doing the exact query you show chazzy. But as i say, it returns a string like "AU ZZ" string when I input "AR" because (I think) the A in AR and AU is enough to satisfy the LIKE.

chazzy
10-19-2006, 10:41 PM
could you post your exact code, because what you are saying is not true.