...

View Full Version : LIKE Clause with php variable



jrsl
04-28-2009, 09:43 AM
Hi people's,

Can somebody help me with this. I cant figure this out and i bet it is realy simple.
How do you include a variable in a LIKE clause?

Here is what i want to do:


SELECT *
FROM `markers`
WHERE radiusType LIKE '%$radius%'


Where $radius is my php variable. I have tried various brackets and " '

Cheers,
Justin

abduraooft
04-28-2009, 09:58 AM
$result=mysql_query("SELECT *
FROM `markers`
WHERE radiusType LIKE '%$radius%'") or die(mysql_error());

jrsl
04-28-2009, 10:06 AM
Thanks for your reply,

Although i just need to know how to include the varable as:


SELECT *
FROM `markers`
WHERE radiusType LIKE '%$radius%'

does not work. it does work when i replace $radius with the value.
and the varable is being passed on correctley as if i use:



SELECT *
FROM `markers`
WHERE radiusType = $radius


works fine.
I just need to know how to include the variable.
Cheers,
Justin

jrsl
04-28-2009, 11:37 AM
Here is my actual query




$query = sprintf("SELECT address, name, lat, lng, ( 6371 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM markers WHERE radiusType LIKE '%{$radius}%' AND $manufacture = '1' AND `$category` = '1' HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",
mysql_real_escape_string($center_lat),
mysql_real_escape_string($center_lng),
mysql_real_escape_string($center_lat),
mysql_real_escape_string($radius));

$result = mysql_query($query);

if (!$result) {
die("Invalid query: " . mysql_error());
}


The $radius part does not work

It works when i do this though:



$query = sprintf("SELECT address, name, lat, lng, ( 6371 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM markers WHERE radiusType = $radius AND $manufacture = '1' AND `$category` = '1' HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",
mysql_real_escape_string($center_lat),
mysql_real_escape_string($center_lng),
mysql_real_escape_string($center_lat),
mysql_real_escape_string($radius));

$result = mysql_query($query);

if (!$result) {
die("Invalid query: " . mysql_error());
}


It also works if i take out the % signs so it says radiusType LIKE '$radius'
i say it dont like them, but that dont help me.

Cheers,
Justin

Fumigator
04-28-2009, 04:23 PM
You're having trouble because the sprintf() function is interpreting the percent signs as replacement tokens. You just need to escape the percent signs by doubling them up like this:



$query = sprintf("SELECT address, name, lat, lng, ( 6371 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance
FROM markers WHERE radiusType LIKE '%%$radius%%' AND $manufacture = '1' AND `$category` = '1' HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",
mysql_real_escape_string($center_lat),
mysql_real_escape_string($center_lng),
mysql_real_escape_string($center_lat),
mysql_real_escape_string($radius));



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum