Page 1 of 1

How can I prevent repeating information... [solved]

Posted: Mon Apr 17, 2006 8:55 am
by joetheeskimo5
Hello,

I'm working on a PHP/mySQL-dependent website that allows visitors to upload their animations for the public.

What I'm trying to do is have a list, from the database, of all the submitters, e.g.

Choose an Animator
Joe
Alex
Mike

The most straightforward way to do this is:

Code: Select all

$sql = "SELECT creator FROM animations";
$result = @mysql_query($sql, $connection);

while ($row = mysql_fetch_array($result)) {
echo $row['animator'];
}
This simple script displays all the names of all the submitters. The only problem is, if Alex submitted three animations, I don't want the script to display his name three times - I just want it once.
So I tried adding this:

Code: Select all

while ($row = mysql_fetch_array($result)) {
if (!eregi($row['animator'], $row) {
echo $row['animator'];
}
}
I know that's probably completely wrong, but I was hoping that, in some warped way, it would detect whether I had already displayed the name. I was wrong, of course.

So, now that I've spent 5 minutes leading up to a really dumb question...how can I just display the person's name once, without any repeats? :oops:

Posted: Mon Apr 17, 2006 9:10 am
by feyd
SELECT DISTINCT....

Posted: Mon Apr 17, 2006 9:12 am
by joetheeskimo5
I knew it would be something really simple.

Posted: Mon Apr 17, 2006 10:09 pm
by evilmonkey
I also have a question about SELECT DISTINCT. My SELECT DISTINCT query strips away everything that is repeating. For example, if I have Alex, Joe, Bob, Alex, Jack, Bill, Alex won't show at all. Is that typical? Any way to get around that? From the MySQL manual:
The ALL, DISTINCT, and DISTINCTROW options specify whether duplicate rows should be returned. If none of these options are given, the default is ALL (all matching rows are returned). DISTINCT and DISTINCTROW are synonyms and specify removal of duplicate rows from the result set.
Am I to interpret that as removing them entirely? That's what mine does...

Posted: Mon Apr 17, 2006 10:39 pm
by feyd
That's what distinct does. It does not add rows that (exactly) match existing resultant row.

Posted: Mon Apr 17, 2006 10:42 pm
by evilmonkey
Okay,. I'll start a new topic when I can coherently phrase this. Good night (It's almost midnite in Toronto :P )