Search Product names in MySQL

Discussion in 'MySQL' started by greatlogix, Sep 25, 2007.

  1. #1
    I am using following query to find product names in products table
    Product in DB is 'Blue Cradle Sheet' when I search "blue cradle" or "cradle sheet" than this product is in search result but when I search for "Cradle blue" than result is zero row returned. What should I change in query that if I search for "Cradle blue" I can get 'Blue Cradle Sheet' in result
     
    greatlogix, Sep 25, 2007 IP
  2. MakeADifference

    MakeADifference Peon

    Messages:
    476
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    if you are using php
    $paramArray = explode(" ",$keywords); //$keywords = "Cradle blue";
    $sql = "";
    for($i = 0; $i < count($paramArray); $i++)
    {
    $param = trim($paramArray[$i]);
    if($i > 0){
    $sql .= ' OR ';
    }

    $sql .= "(product_name LIKE lower('%$param%'))";

    }

    echo $sql;

    Basically you need to build your query dynamically. There may be errors in the code that I wrote but I just wanted to give you an idea.

    Cheers
     
    MakeADifference, Sep 25, 2007 IP
  3. greatlogix

    greatlogix Active Member

    Messages:
    664
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    85
    #3
    Not working.

    I have following products

    Cradle - Royal Blue Woven
    Round Crib - Blue Gingham Jersey Knit
    Cradle - Solid Blue Woven

    Now If I search for 'Cradle blue' or 'Blue Cradle' I want "Cradle - Solid Blue Woven" and "Cradle - Royal Blue Woven" in result
    Although "Round Crib - Blue Gingham Jersey Knit" has keyword blue in it but its not cradle so it should not be in result. How can I do this?
     
    greatlogix, Sep 27, 2007 IP
  4. jakomo

    jakomo Well-Known Member

    Messages:
    4,262
    Likes Received:
    82
    Best Answers:
    0
    Trophy Points:
    138
  5. greatlogix

    greatlogix Active Member

    Messages:
    664
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    85
    #5
    I tried following query

    I was expecting "Cradle - Solid Blue Woven" in result but result set was zero row.

    What I am doing wrong in this query?
     
    greatlogix, Sep 28, 2007 IP
  6. Jamie18

    Jamie18 Peon

    Messages:
    201
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    what you're going to want to do is split the query string up.. i don't use php so i can't really help you there.. but after that you'd have put a loop in the where condition..

    this pseudo should get you on track i think
    i.e.

    $keyword is an array holding each individual search word..

    where product_name LIKE '%$keyword[0]%'
    php loop from 1 to keyword.length index=i
    AND product_name LIKE '%$keyword%'
    end loop

    this way in order to be returned by the query, product_name must include all the words from the keyword array
     
    Jamie18, Sep 28, 2007 IP