Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    LIKE Clause with php variable

    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:
    Code:
    SELECT *
    FROM `markers`
    WHERE radiusType LIKE '%$radius%'
    Where $radius is my php variable. I have tried various brackets and " '

    Cheers,
    Justin

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,866
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    PHP Code:
    $result=mysql_query("SELECT *
    FROM `markers`
    WHERE radiusType LIKE '%$radius%'"
    ) or die(mysql_error()); 
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for your reply,

    Although i just need to know how to include the varable as:
    Code:
    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:

    Code:
    SELECT *
    FROM `markers`
    WHERE radiusType = $radius
    works fine.
    I just need to know how to include the variable.
    Cheers,
    Justin

  • #4
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Here is my actual query

    Code:
    $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:
    Code:
    $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
    Last edited by jrsl; 04-28-2009 at 12:24 PM.

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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:

    PHP Code:
    $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)); 

  • Users who have thanked Fumigator for this post:

    jrsl (04-29-2009)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •