www.webdeveloper.com
Results 1 to 7 of 7

Thread: [RESOLVED] Displaying data in left join

  1. #1
    Join Date
    Nov 2009
    Posts
    97

    resolved [RESOLVED] Displaying data in left join

    Hello everyone,

    Hopefully this is a quick one, I need help with the following code:

    PHP Code:
    <select name="town" id="county">
        <option value=""></option>
        <?php $data $conn->query("SELECT CDV.id, CDV.name, gTowns.gtID, gCounties.id FROM CDV LEFT JOIN gTowns ON CDV.town=gTowns.gtID LEFT JOIN gCounties ON gTowns.county=gCounties.id ORDER BY name ASC");
        while (
    $row $data->fetch(PDO::FETCH_ASSOC)){ ?>
            <option value="<?php echo $row['id']; ?>"><?php echo $row['name'].' ('.$towns[$row['gtID']].', '.$counties[$row['id']].')'?></option>
        <?php ?>
    </select>
    This is my question:

    Here
    PHP Code:
    <option value="<?php echo $row['id']; ?>">
    The code is supposed to display the id from the CDV table and it is displaying the id from the gCounties instead (both tables have an "id" column).

    Is there a way I can tell php that I want the id from x table displayed instead of the other one?

    Thanks for any help provided

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    555
    You have duplicate names in your results. In your selection list add an "as xxx" to the fields that are named the same so you can accurately reference them in the results.

  3. #3
    Join Date
    Nov 2009
    Posts
    97
    Thanks for the help ginerjm,

    I'm not sure I get what you're saying, could you post an example to clear my doubts?

    Thanks again!

  4. #4
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    555
    You do know that there are plenty of references to sql syntax out there?

    select a.id as aid, b.id as bid

    Refer to them as $row['aid'] & $trow['bid'] in the results.

  5. #5
    Join Date
    Nov 2009
    Posts
    97
    There are online references just about any topic in the world but sometimes you just don't know the right words to begin your search, I did try looking for an answer in Google but I just had no idea that it was this way (otherwise I wouldn't have asked here).

    Anyways, that did it, it's working just the way I needed it, thanks so much for the help.

  6. #6
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    555
    Glad to help you but I don't buy your excuse. Simply google MySQL syntax and what do you get? The first three results (with G.) are links to the MySQL.com manual.

  7. #7
    Join Date
    Nov 2009
    Posts
    97
    I don't think I need an excuse to ask a question here nor did I provide one when I first created the new topic. What you see as my excuse is just my response to your otherwise rhetorical question; it is the equivalent of me asking you why bother helping others with an answer if you know everybody has the response just a search query and a few clicks away?

    Again, I appreciate the help.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles