I have this query which searches articles in a db. I would like to change the query so that if the keyword is found in the "title
" row, then the query should sort by that first - in order to put it in the top of the results.
Results with the keyword in the title
should be considered more important than if the keyword is in the content of an article.
Can this be achieved in the query itself, or does it require some sort of algorithm?
Here's my query:
if (isset($_POST['search'])) {
$search = filter_var($search, FILTER_SANITIZE_SPECIAL_CHARS);
$sql = "SELECT * FROM mydb WHERE title LIKE :search OR description LIKE :search OR content LIKE :search ORDER BY dates DESC";
$stmt = $pdo->prepare($sql);
$stmt->bindValue(':search', '%' . $search . '%', PDO::PARAM_STR);
$stmt->execute();
if ($stmt->rowCount() > 0) {
$result = $stmt->fetchAll();
foreach($result as $row) {
// Loop...
}
}
else {
}
// Closing
$stmt = null;
$pdo = null;
}