For with duplicated result

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vitorcardieri
Forum Newbie
Posts: 1
Joined: Thu Dec 18, 2008 9:35 pm

For with duplicated result

Post by vitorcardieri »

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>";
} }
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: For with duplicated result

Post by requinix »

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
Post Reply