Search code examples
phpmysqlsearchrelevance

How to make mysql automatically bold matching words in fulltext search


I have tried to precisely explain my problem in the title itself. Can I surround the matching words in query by in mysql query itself? MySQL query is:

select id, FirstName,LastName,addcomments WHERE MATCH (FirstName,LastName,addcomments) AGAINST ('some sample text' WITH QUERY EXPANSION)

The results from mysql should be like:

id | FirstName       | LastName             | addcomments 

1  |<b>some</b> name | lastname <b>text</b> | comments have <b>some sample text</b> inside

Any help would be deeply appreciated


Solution

  • Thanks kris for the reply. I agree mysql will return generic results that can be consumed by any application. But can I write a mysql function for the same effect?

    Also, I tried writing php function for that, but its not working:

    $searchitems=explode(" ", $trimmed);
    $totalSearchItems = count($searchitems);
    
    while ($row= mysql_fetch_array($result))
    {
          for($i=0;$i<$totalSearchItems;$i++)
          {
                str_replace($searchitems[$i], '<b>'.$searchitems[$i].'</b>', $row);
          }
          //Display $row
    }