SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict bronze trophy
    Join Date
    Sep 2005
    Posts
    317
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)

    Searching 10% in mysql

    I'm trying to allow users to search for items in my table that begin with 10%. However when a user searchs for 10%, it justs acts like: Find all words beginning with 10 and anything after, which isn't what I want ^_^. I know the % is used to represent any amount of characters after, so I'm wondering how to make % a literal character :X?

    Any help would be greatly apprciated ^_^

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Replace occurances of % with \%:
    PHP Code:
    <?php
    $input 
    '10%';
    $input str_replace('%''\%'$input);
    mysql_query("SELECT .... FROM .... WHERE .... LIKE '{$input}%'");
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    Twitter: @TimIgoe silver trophy TimIgoe's Avatar
    Join Date
    Feb 2005
    Location
    Blackpool, UK
    Posts
    1,056
    Mentioned
    27 Post(s)
    Tagged
    1 Thread(s)
    Because &#37; is the wildcard character, just like you'd use " or ' to contain strings, you need to then escape the wildcard to force mysql to see it as a literal character.

    As arkinstall says, turn % into \% in your query.

  4. #4
    SitePoint Addict bronze trophy
    Join Date
    Sep 2005
    Posts
    317
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Thanks Works like a treat!


Bookmarks

Posting Permissions

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