开发者

Searching by each word rather than complete string using MYSQL PHP

开发者 https://www.devze.com 2023-04-10 10:04 出处:网络
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 o

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);
  开发者_StackOverflow社区      $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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号