Search code examples
phpmysqlirelated-content

How to find related post from title and content - PHP and Mysqli


I am trying to fetch related contents from Mysqli DB. It is easy to fetch by Like %%, but that doesn't output required related contents.

I want to have related post, similar to title and content.

Structure: title - text and content - long text

Here is the code:

<?php
$sql = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT *, MATCH(title, content) AGAINST('$CurrentPostTitle') AS score
FROM articles 
WHERE MATCH(title, content) AGAINST('$CurrentPostTitle') 
ORDER BY score DESC LIMIT 4");
while ($row = mysqli_fetch_array($sql)) {
$title = $row['title'];
echo $title;
echo "\r\n";
}
?>

But that doesn't output anything?


Solution

  • enable Full Text for title and use this code: its pesudo code

    SELECT *, MATCH(title, body) AGAINST('$CurrentPostTitle') AS score
    FROM articles 
    WHERE MATCH(title, body) AGAINST('$CurrentPostTitle') 
    ORDER BY score DESC LIMIT 5
    

    i hope to help you