Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6218655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:38:49+00:00 2026-05-24T07:38:49+00:00

I have two primary MySQL tables (profiles and contacts) with many supplementary tables (prefixed

  • 0

I have two primary MySQL tables (profiles and contacts) with many supplementary tables (prefixed by prm_). They are accessed and manipulated via PHP.

In this instance I am querying the profiles table where I will retrieve an Owner ID and a Breeder ID. This will then be referenced against the contacts table where the information on the Owners and Breeders is kept.

I received great help here on another question regarding joins and aliases, where I was also furnished with the following query. Unfortunately, I am having huge difficulty in actually echoing out the results. Every single site that deals with Self Joins and Aliases provide lovely examples of the queries – but then skip to “and this Outputs etc etc etc”. How does it output????

SELECT *
FROM (
      SELECT *
         FROM profiles
              INNER JOIN prm_breedgender
                  ON profiles.ProfileGenderID = prm_breedgender.BreedGenderID
              LEFT JOIN contacts ownerContact
                  ON profiles.ProfileOwnerID = ownerContact.ContactID
              LEFT JOIN prm_breedcolour
                  ON profiles.ProfileAdultColourID = prm_breedcolour.BreedColourID
              LEFT JOIN prm_breedcolourmodifier
                  ON profiles.ProfileColourModifierID = prm_breedcolourmodifier.BreedColourModifierID
) ilv LEFT JOIN contacts breederContact
     ON ilv.ProfileBreederID = breederContact.ContactID
WHERE ProfileName != 'Unknown'
ORDER BY ilv.ProfileGenderID, ilv.ProfileName ASC $limit

Coupled with this is the following PHP:

$owner = ($row['ownerContact.ContactFirstName'] . ' ' . $row['ownerContact.ContactLastName']);
$breeder = ($row['breederContact.ContactFirstName'] . ' ' . $row['breederContact.ContactLastName']);

All details EXCEPT the contacts (gender, colour, etc.) return fine. The $owner and $breeder variables are empty.

Any help in settling this for me would be massively appreciated.

EDIT: My final WORKING query:

SELECT ProfileOwnerID, ProfileBreederID, 
        ProfileGenderID, ProfileAdultColourID, ProfileColourModifierID, ProfileYearOfBirth, 
        ProfileYearOfDeath, ProfileLocalRegNumber, ProfileName,
        owner.ContactFirstName AS owner_fname, owner.ContactLastName AS owner_lname,
        breeder.ContactFirstName AS breeder_fname, breeder.ContactLastName AS breeder_lname,
        BreedGender, BreedColour, BreedColourModifier

        FROM profiles
                    LEFT JOIN contacts AS owner
                        ON ProfileOwnerID = owner.ContactID
                    LEFT JOIN contacts AS breeder
                        ON ProfileBreederID = breeder.ContactID
            LEFT JOIN prm_breedgender
                        ON ProfileGenderID = prm_breedgender.BreedGenderID
                    LEFT JOIN prm_breedcolour
                        ON ProfileAdultColourID = prm_breedcolour.BreedColourID
                    LEFT JOIN prm_breedcolourmodifier
                        ON ProfileColourModifierID = prm_breedcolourmodifier.BreedColourModifierID

                  WHERE ProfileName != 'Unknown'
            ORDER BY ProfileGenderID, ProfileName ASC $limit

Which I could then output by:

$owner = ($row['owner_lname'] . ' - ' . $row['owner_fname']);

Many Thanks to All!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:38:49+00:00Added an answer on May 24, 2026 at 7:38 am

    I guess you’re using the mysql_fetch_array or the mysql_fetch_assoc-functions to get the array from the result-set?

    In this case, you can’t use

    $row['ownerContact.ContactFirstName']
    

    as the PHP-Docs read:

    If two or more columns of the result have the same field names, the
    last column will take precedence. To access the other column(s) of the
    same name, you must use the numeric index of the column or make an
    alias for the column. For aliased columns, you cannot access the
    contents with the original column name.

    So, you can either use an AS in your SQL-query to set other names for the doubled rows or use the numbered indexes to access them.


    This could then look like this:

    Using AS in your Query

    In your standard SQL-query, the columns in the result-set are named like the columns which their values come from. Sometimes, this can be a problem due to a naming-conflict. Using the AS-command in your query, you can rename a column in the result-set:

    SELECT something AS "something_else"
    FROM your_table
    

    This will rename the something-column to something_else (you can leave the ""-quotes out, but I think it makes it more readable).

    Using the column-indexes for the array

    The other way to go is using the column-index instead of their names. Look at this query:

    SELECT first_name, last_name
    FROM some_table
    

    The result-set will contain two columns, 0 ==> first_name and 1 ==> last_name. You can use this numbers to access the column in your result-set:

    $row[0] // would be the "first_name"-column
    $row[1] // would be the "last_name"-column
    

    To be able to use the column-index, you’ll need to use mysql_fetch_row or the mysql_fetch_assoc-function, which offers an associative array, a numeric array, or both (“both” is standard).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two MySQL (MyISAM) tables: Posts: PostID(primary key), post_text, post_date, etc. Comments: CommentID(primary
How can I merge two MySQL tables that have the same structure? The primary
I have two tables with primary foreign key relationship. The table in foreign key
i have two tables both are related with primary-foreign key ralation and i have
I have two SQL Server tables with Primary Keys (PK) and a Foreign Key
I have two tables, the first has a primary key that is an identity,
I have two database tables with the following structure: actions: action_id int(11) primary key
I have the following two mySQL tables with the 'child' table having a DELETE
I have two tables in a MySQL database, Locations and Tags, and a third
I have two tables, structured like so: table A: A_ID varchar(32) PRIMARY KEY field1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.