I have this code below that gets a sequence of words, divide usin explode and for each word makes a select
on the database... ok!
But the problem is: i have in the field name on my db for example: John Horn Cobra
and the variable $word = John Cobra
so: $word[0] = John and $word[1] = Cobra
Then i'm having trouble because those two variables are finding the same register (id) and i'm having it showing duplicated!
How can i show only results that are not duplicated (the $id)?
HERE'S THE CODE!!! THANKS!!!
$words = explode(" ", $frase); // Slice $frase using " " (espace) as divisor
for($i=0;$i<count($words);$i++)
{
$sql_show = "SELECT * FROM actor WHERE name LIKE '%$words[$i]%' or id LIKE '%$words[$i]%'";
$result = mysql_query($sql_show);
while ($linha = mysql_fetch_array($result)) {
$name = $linha['name'];
$id = $linha['id'];
echo"$id - $name<br>";
} }
For with duplicated result
Moderator: General Moderators
Re: For with duplicated result
Adding a GROUP BY clause will sort the results and only keep the distinct ones.
Code: Select all
SELECT * FROM actor WHERE name LIKE '%$words[$i]%' or id LIKE '%$words[$i]%' GROUP BY name