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

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
joetheeskimo5
Forum Commoner
Posts: 43
Joined: Sun Dec 14, 2003 4:47 pm
Location: US
Contact:

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

Post 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:
Last edited by joetheeskimo5 on Mon Apr 17, 2006 9:12 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

SELECT DISTINCT....
joetheeskimo5
Forum Commoner
Posts: 43
Joined: Sun Dec 14, 2003 4:47 pm
Location: US
Contact:

Post by joetheeskimo5 »

I knew it would be something really simple.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

That's what distinct does. It does not add rows that (exactly) match existing resultant row.
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Okay,. I'll start a new topic when I can coherently phrase this. Good night (It's almost midnite in Toronto :P )
Post Reply