M1X
M1X

Reputation: 5354

Full text search with correct words only

I'm coding a search function using full-text-search and I want result to be shown only if the words typed are contained in table.

$search_value = $db->mysqli->real_escape_string("lana born to die sdkjfhsdkjfhdsjkfhdsjkfhd");

$query = "select * from `music` where match(`title`, `artist`) against ('$search_value')";


artist               title
----------------------------------
lana del rey    west coast
lana del rey    born to die
lana del rey    summertime sadness
lana del rey    blue jeans

For ex. if I search: lana born to die sdkjfhsdkjfhdsjkfhdsjkfhd it shows me lana del rey born to die as a result. This should not happen. I want it to show results only if you type for ex. lana, lana del rey born and so on.

Any ideas.

Upvotes: 0

Views: 84

Answers (2)

Hardik Solanki
Hardik Solanki

Reputation: 3195

You need to correct your query :

$query = "select * from `music` where `title` LIKE '%$search_value%'  
          OR `artist` LIKE '%$search_value%'";

I hope that will be help for you.

Upvotes: 0

Cedric Le Varlet
Cedric Le Varlet

Reputation: 99

You could transform the query to have a boolean full-text search. This is done by adding a + or - in front of every word within the search query.

In this case, we add a + as this stands as an AND whereas a - stands as a NOT.

As such if you want to have it such that every word becomes required you could add the following code:

PHP:

$search_value = $db->mysqli->real_escape_string("lana born to die sdkjfhsdkjfhdsjkfhdsjkfhd");
$search_value = str_replace(" ", " +", $search_value);
$query = "select * from `music` where match(`title`, `artist`) against ('$search_value' IN BOOLEAN MODE)";

Upvotes: 3

Related Questions