What am I missing with this query?

Posted on 2011-05-10
Last Modified: 2012-08-14
This code works:

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

But when I try to retrieve the value using:

echo $row[''], 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 142

    Accepted Solution

    you have to use aliases:
    $querystate = "select 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 = 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 playlist_id, pl.contestant_id, pl.playlist_name, registration_id, r.first_name, r.last_name, r.song_title from download_playlist pl, registration r where
                                  pl.contestant_id = order by r.song_title"; 

    Open in new window

    LVL 107

    Assisted Solution

    by:Ray Paseur
    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


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

    $querystate = "SELECT AS download_id, download_playlist.contestant_id AS download_contestant_id, download_playlist.playlist_name AS download_playlist_name, 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 AS dp_id, dp.contestant_id AS dp_contestant_id, dp.playlist_name AS dp_playlist_name, 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,

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Suggested Solutions

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    758 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now