I am attempting to display a distinct set of rows in PHP based on the results of a MySQL query, but I am seeing duplicate records output to the HTML.
My PHP and MySQL code looks like this:
<h2>Related products</h2>
<?php
$title=str_replace(' ',',',$_GET['title']);
$words=explode(',',$title);
foreach ($words as $word){
if (strlen($word) > 5){
$res=mysqli_query($link, "SELECT title,id FROM posts WHERE title LIKE '% $word %' or title LIKE '%$word' or title LIKE '$word%' ORDER BY id DESC");
while ($row=mysqli_fetch_array($res)){
echo "<a href='/product/".$row['id']."/'><h3>".$row['title']."</h3></a>";
}}}
?>
I searched for similar questions already asked, and based on their guidance I tried applying:
Select DISTINCT..
As well as:
while ($row=array_unique(mysqli_fetch_array($res)))
It is still showing duplicate results, however. (The same product shows up 3-5 times in the related products section.)