What am I missing with this query?

This code works:

$querystate = "select download_playlist.id, download_playlist.contestant_id, download_playlist.playlist_name, registration.id, registration.first_name, registration.last_name, registration.song_title from download_playlist, registration where
                              download_playlist.contestant_id = registration.id order by registration.song_title";

But when I try to retrieve the download_playlist.id value using:

echo $row['download_playlist.id'], I get nothing.

If I put, echo $row['id'], I'll get the id from the registration table, but I need to get the id from the download_playlist table. How?
brucegustPHP DeveloperAsked:
Who is Participating?
Guy Hengel [angelIII / a3]Billing EngineerCommented:
you have to use aliases:
$querystate = "select download_playlist.id playlist_id, download_playlist.contestant_id, download_playlist.playlist_name, registration.id registration_id, registration.first_name, registration.last_name, registration.song_title from download_playlist, registration where
                              download_playlist.contestant_id = registration.id order by registration.song_title"; 

Open in new window

and then you have use $row["playlist_id"] and $row["registration_id"] without any problems.

talking aliases:

$querystate = "select pl.id playlist_id, pl.contestant_id, pl.playlist_name, r.id registration_id, r.first_name, r.last_name, r.song_title from download_playlist pl, registration r where
                              pl.contestant_id = r.id order by r.song_title"; 

Open in new window

Ray PaseurCommented:
Consider adding error_reporting(E_ALL) to your scripts and also testing the return value from the query.

if (!$querystate) die(mysql_error());

You can also use var_dump($row) to find out what you did get back.   Easier than guessing at what the names in that array might be!

You could give each field an alias making it easy to see what your calling latter, so something like...

$querystate = "SELECT download_playlist.id AS download_id, download_playlist.contestant_id AS download_contestant_id, download_playlist.playlist_name AS download_playlist_name, registration.id AS registration_id, registration.first_name AS registration_name, registration.last_name AS registration_last_name, registration.song_title AS registration_song_title FROM download_playlist, registration;

Open in new window

And then echo the row like this...

echo $row['download_id'];

Open in new window

Or for it to be quicker and easier to understand do as angellll said and define the table aswell...

$querystate = "SELECT dp.id AS dp_id, dp.contestant_id AS dp_contestant_id, dp.playlist_name AS dp_playlist_name, r.id AS r_id, r.first_name AS r_first_name, r.last_name AS r_last_name, r.song_title AS r_song_title FROM download_playlist AS dp, registration AS r;

Open in new window

Then echo it...

echo $row['dp_id'];

Open in new window

Hope this helps,
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.