Filtering multiple results in MySQL result array
Posted: Mon Jan 25, 2010 7:41 pm
Greets,
I'm currently working on a music related website. I need to list all albums by an artist, but the problem is, each song is an individual record in the database. So I'm doing a query to fetch all songs, and I need to just take one of each name out of the album field. If I haven't already confused you enough, here's my attempt at an example:
Ok, so there's an example of what the table looks like. Here's the PHP I have written:
Or it's something along those lines..
It all comes down to this.. how do I make an array that contains just the album names without duplicates? So $albums[] will contain Blue, Green instead of Blue, Blue, Blue, Blue, Green .... etc
I'm currently working on a music related website. I need to list all albums by an artist, but the problem is, each song is an individual record in the database. So I'm doing a query to fetch all songs, and I need to just take one of each name out of the album field. If I haven't already confused you enough, here's my attempt at an example:
Code: Select all
|-----Album-----|-----Artist-----|-----Song-----|
| Blue | Band | Song 1 |
| Blue | Band | Song 2 |
| Blue | Band | Song 3 |
| Blue | Band | Song 4 |
| Green | Band | Song 1 |
| Green | Band | Song 2 |
| Green | Band | Song 3 |
| Green | Band | Song 4 |
|------------------------------------------------|
Code: Select all
<?php
$artist = $_GET['artist']; // Band name retrieved from $_GET
$q = "SELECT * FROM songlist WHERE Band='$artist'";
$r = mysqli_query($db, $q); // db link from included file
$row = mysqli_fetch_array($r);
?>
It all comes down to this.. how do I make an array that contains just the album names without duplicates? So $albums[] will contain Blue, Green instead of Blue, Blue, Blue, Blue, Green .... etc