simple mysql problem

Discussion in 'MySQL' started by dean5000v, May 13, 2008.

  1. #1
    ok well i have this coding working

    $query = mysql_query("SELECT * FROM stock WHERE item_code LIKE '%$search%'");


    but i dont want it to be LIKE i want it to be exact so i try

    $query = mysql_query("SELECT * FROM stock WHERE item_code = '%$search%'");

    but it doesn't work, can anyone think why ???
     
    dean5000v, May 13, 2008 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    When you do pattern matches you need to use LIKE or NOT LIKE. This is a built in feature of mysql that lets it know you need to match a pattern.

    http://dev.mysql.com/doc/refman/5.0/en/pattern-matching.html

    You either need to do:

    mysql_query("SELECT * FROM stock WHERE item_code LIKE '%$search%'");

    or

    mysql_query("SELECT * FROM stock WHERE item_code = '$search'");
     
    jestep, May 13, 2008 IP
  3. gauravgrt

    gauravgrt Peon

    Messages:
    2,035
    Likes Received:
    129
    Best Answers:
    0
    Trophy Points:
    0
    #3
    When using like operator there is no need to use "=" sign.
     
    gauravgrt, May 14, 2008 IP
  4. mrphp

    mrphp Well-Known Member

    Messages:
    682
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    110
    #4
    he says he wants to be exact then the code is

    $query = mysql_query("SELECT * FROM stock WHERE item_code='$search'");

    also please do not use * in searching use index for faster result.
     
    mrphp, May 18, 2008 IP
  5. bluecape

    bluecape Peon

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Try this method
    $query = mysql_query("SELECT * FROM stock WHERE item_code = '%".$search."%'");
    Code (markup):
    edit: sorry i didnt read your question carefuly, please disregard this post
     
    bluecape, May 22, 2008 IP