how to make mysql display one of each value

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
User avatar
paqman
Forum Contributor
Posts: 125
Joined: Sun Nov 14, 2004 7:41 pm
Location: Burnaby, BC, Canada

how to make mysql display one of each value

Post by paqman »

If an array has duplicate values (a, a, b, b, b, c, d, d) is there any way to make it only have one of each? (a, b, c, d). Thanks!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

using the GROUP BY aggregate system or SELECT DISTINCT .. depending on your query.
User avatar
paqman
Forum Contributor
Posts: 125
Joined: Sun Nov 14, 2004 7:41 pm
Location: Burnaby, BC, Canada

Post by paqman »

okay distinct sort of worked... I have 3 entries to test it, and 2 are the same. I put it all in and I only get 2 results (it's working!) but they say: Resource id #4... Anyone ever heard of this before? Here's my code:

Code: Select all

$title_xbox = mysql_query("select distinct title from review where subsystem='xbox'");
$num_xbox=mysql_numrows($title_xbox);


echo "<h4>Xbox:</h4>";
while($r=mysql_fetch_array($title_xbox))
{
echo "<a href=\"reviews.php?cmd=view&title=$title_xbox\">$title_xbox</a><br>";
}
if ($num_xbox == 0) {
echo "No Xbox Reviews Found<br><br>";
}
Thanks for the help!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

echo "<a href=\"reviews.php?cmd=view&title={$r['title']}\">{$r['title']}</a><br>";
User avatar
paqman
Forum Contributor
Posts: 125
Joined: Sun Nov 14, 2004 7:41 pm
Location: Burnaby, BC, Canada

solved

Post by paqman »

thanks so much!
User avatar
paqman
Forum Contributor
Posts: 125
Joined: Sun Nov 14, 2004 7:41 pm
Location: Burnaby, BC, Canada

Post by paqman »

okay 1 more thing added on to that. Is there any way to get other values from the rows when using distinct? Or is there some way to do a second query to get the other values? here's the code I have so far:

Code: Select all

$exact_results = mysql_query("select distinct title from review where title='$title' and subsystem='$subsystem'");

while($r=mysql_fetch_array($exact_results))
{
$e_title = $r["title"];
$e_subsystem = $r["subsystem"];

echo "<tr><td><a href=\"index.php?id=submit&cmd=form&title=$e_title&subsystem=$e_subsystem\">$e_title</a></td><td>$e_subsystem</td></tr>";
}
What I would like is to be able to get the value 'subsystem' from the table. Thanks so much!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

select distinct title, subsystem from review where subsystem='xbox'
Post Reply