I have a search form on my website that is suppose to search article titles in my table for matching words. It seems to be working for certain strings but not others and I'm not sure why. For instance if you search for "assassin's creed" the search function return the right rows from the table. But if you search for "the last of us", there are no results, even though there is an article title with that string in it.
Below is my PHP/SQL command. Is there anything I need to add/change? Do I need to run a FULLTEXT index on the table's column each time I run a query? This is the first time I've used MySQL's MATCH AGAINST functionality. Usually I would use LIKE but after doing a little reading, apparently MATCH AGAINST is a much better approach.
public function fetchSearchArticles($q, $category, $platform, $sort) {
$q = addslashes($q);
$category = ($category != 'all')? ' AND `cat_id` = '.$this->fetch_category_id($category).' ' : '';
$platform = ($platform != 'all')? ' AND `'.str_replace('-', '_', $platform).'` = 1 ': '';
$sort = ($sort != 'rel')? '`article_timestamp` '.strtoupper($sort).'' : "MATCH(`article_title`) AGAINST('$q')";
$query = $this->db->prepare ("
SELECT *
FROM articles
WHERE `article_is_published` = 1
$platform
$category
AND MATCH(`article_title`) AGAINST('+$q' IN BOOLEAN MODE)
ORDER BY $sort
LIMIT 20
");
try{
$query->execute();
$articles = $query->fetchAll(PDO::FETCH_ASSOC);
$query->closeCursor();
return $articles;
} catch(PDOException $e){
die($e->getMessage());
}
}