Dan
Dan

Reputation: 3815

Searching by each word rather than complete string using MYSQL PHP

My search allows users to search games in my database. The only problem is that if a user types multiple terms within the search, it will only get hits if those multiple words are right next to each other.

Example:

searched term = Call Duty Modern

result = none

BUT if they input:

searched term = Call of Duty -or- Modern Warfare

result = Call of Duty Modern Warfare 1/2/3 etc.

here is my mysql query:

$query = "SELECT ID, title FROM games WHERE title LIKE '%{$title}%' AND platform = :platform LIMIT $startFrom,15";
        $statement = $dbG->prepare($query);
        $statement->bindValue(':platform', $platform);
        $statement->execute();
        $gamesLike = $statement->fetch(PDO::FETCH_ASSOC);

I know i should break up each word and search each term but im afraid it would eat up all my programs speed...

are there any specific MYSQL query tweaks i can use to achieve the result i need?

Any suggestions would be greatly appreciated

Thank you for your time

Upvotes: 3

Views: 3011

Answers (1)

Jonathan Kuhn
Jonathan Kuhn

Reputation: 15301

full text searching is great for this: http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html

It essentially turns mysql into a search engine where you use match and against to get a rank back of how often the search terms show up in the column you are matching against. It can handle partial matches, multiple matches, excludes words that are small/common to help performance and allows for search keywords/symbols like +some -search will return results that must have some but can't have search.

Also it is surprisingly easy to setup as pretty much everything you need works by default.

Upvotes: 6

Related Questions