php mysql select query problem.

Discussion in 'PHP' started by Ntech, Aug 8, 2009.

  1. #1
    Hi all,

    I'm using the query below to find all rows in my database where the number 26 appears in a particular string of comma separated numbers such as: (1,4,8,25,26,36,136,145)

    $matches = mysql_query("SELECT * FROM allitems WHERE allgenres LIKE '%26%'");
    Code (markup):
    But it's not just returning the rows where the number 26 appears but also the rows where the number 126 or the number 226 appear and so on.

    How do I limit it to just the number 26?
     
    Ntech, Aug 8, 2009 IP
  2. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Maybe %,26,% instead, since % is basically a wild card, so obviously 126 and 226 would be true. So you might have to do something like

    WHERE allgenres LIKE '26,%' OR allgenres LIKE '%,26,%' OR allgenres LIKE '%,26' OR allgenres = '26'

    Just to cover those variations.
     
    kblessinggr, Aug 8, 2009 IP
  3. Ntech

    Ntech Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    That did it, thanks a lot, kblessinggr.
     
    Ntech, Aug 8, 2009 IP