views:

59

answers:

1

Hello all! I have tried to implement a simple search engine for my application. This is what I have done:

    CREATE FULLTEXT INDEX item_name_other_name_desc_index
    ON item (name,other_name,description)

public static function Search($string)
{   

    $delims = ',.; ';
        $word = strtok($string,$delims);
        while($word)
        {

           $result['accepted'][] = $word;                   
           $word = strtok($delims);

        }

          $string = implode(" ", $result['accepted']);

    $sql = 'SELECT item_id,name,other_name,description,price,discounted_price, thumbnail_photo,large_photo
    FROM item
    WHERE 
    MATCH(name,other_name,description)
    AGAINST(:string)' ;
    $parameters = array(':string' => $string);
    $result['items'] = DB::GetAll($sql,$parameters);
    return $result;
}

This works and searches for words only. Can anyone suggest me how can i improve it and how do i proceed if i want to search for strings like "Bi" which will show me results starting with that string.(for eg Bike..) Thanks!:)

A: 

you can use wildcards like '%' and '_' ... for your example, having a search key like Bi% will include Bike and any other words that starts with 'Bi' in the result set.... and so on...

ultrajohn
Will it be something like :WHERE MATCH(name,other_name,description)AGAINST(:string)AND name LIKE $string% ;
chupinette
in SQL something like, WHERE key = 'Bi%', so from you example, i think you should append/concatenate '%' to $string like ... LIKE $string.'%' ...
ultrajohn
AND name LIKE ' . $string . '%'; Im getting the error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%' at line 6. Can you help me please?
chupinette
you forget to enclose the $string in qoutes, ... as a final sql expression, it should be , AND name like '".$string."%'" so it will appear as, AND name like 'valueOf$string%';
ultrajohn