What am I missing with this query?

Posted on 2011-05-10
Medium Priority
Last Modified: 2012-08-14
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?
Question by:brucegust
LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 1000 total points
ID: 35728588
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

LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
ID: 35728622
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!

Assisted Solution

echocpt earned 800 total points
ID: 35728933

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,

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question