How to display content from MYSQL Join if ID is not relational

I have an artist page that is supposed to show the albums released by a particular artist.

I am joining two tables to accomplish this. The relation is the artist_name. Both the artist table and the albums tables have different IDs.

Unfortunately, the artist page is showing ALL of the album photos in the database instead of only the ones related to the artist.

My thought process was to to use the $_GET method to grab the artist name which would be included in the URL when the user clicks the link to visit that artist’s page. But obviously, this is not working.

Here’s my script:

$artist_name = $_GET['artist_name'];
//Query the database to show the albums
$albums =  "SELECT artist.artist_name,albums.artist_name,albums.photo,albums.album_title
            FROM artist,albums 
            WHERE  artist.artist_name=albums.artist_name
            ORDER BY year_released ASC"; 

$q = $db->query($albums);
$q->setFetchMode(PDO::FETCH_ASSOC);

html:

 <?php while ($r = $q->fetch()): ?>
                    <span class="margin-two album">

                        <img src="http://mywebsite.com/albums/<?php echo $r['photo']; ?>" alt="picture of the album <?php echo $r['album_title']; ?> by <?php echo $r['artist_name']; ?>" title="<?php echo $r['album_title']; ?>"  \>


                    </span>
                    <?php endwhile; ?>

How can I output the correct albums for each artist?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s