Jump to content

Recommended Posts

Hi - wondering if someone could help me out here...

 

I've tried both:

 

SELECT listings.*, states_provinces.*, states_provinces_0.* FROM ((listings LEFT JOIN states_provinces ON states_provinces.StateID=listings.location1provstate) LEFT JOIN states_provinces AS states_provinces_0 ON states_provinces_0.StateID=listings.location2provstate)

 

and

 

SELECT listings.*, states_provinces.*, states_provinces_0.* FROM listings LEFT JOIN states_provinces ON states_provinces.StateID=listings.location1provstate LEFT JOIN states_provinces AS states_provinces_0 ON states_provinces_0.StateID=listings.location2provstate

 

But neither

 

<?php echo $row_Recordset1['states_provinces.StateName']; ?>

or

<?php echo $row_Recordset1['states_provinces_0.StateName']; ?>

give me anything!

 

I know the data is good in the lookup table and <?php echo $row_Recordset1['listings.locationXprovstate']; ?> returns the right StateID values.

 

What the heck am I doing wrong? Many thanks for any assistance.

 

Link to comment
https://forums.phpfreaks.com/topic/223772-syntax-of-2-left-joins-and-alias/
Share on other sites

How are the results listed? As in what are the names that phpMyAdmin returns in the table header of each field? You should be able to use those same names as the array indices when referencing the values.

Thank you for taking the time to help me Pikachu

 

Both headers are named StateName but when I use <?php echo $row_Recordset1['StateName']; ?><?php echo $row_Recordset1['StateName']; ?> I get the same value for both (and the first one is correct).

 

But when I try <?php echo $row_Recordset1['states_provinces.StateName']; ?><?php echo $row_Recordset1['states_provinces_0.StateName']; ?> I get nothing for either.

OK, time to get smarter than the query, LOL. The result of the print_r() will show you how they are indexed in the array.

 

$query = "SELECT listings.*, states_provinces.*, states_provinces_0.* FROM listings LEFT JOIN states_provinces ON states_provinces.StateID=listings.location1provstate LEFT JOIN states_provinces AS states_provinces_0 ON states_provinces_0.StateID=listings.location2provstate";
$result = mysql_query($query);
$array = mysql_fetch_assoc($result);
echo '<pre>';
print_r($array);
echo '</pre>';

 

P.S. Since it appears this is ultimately a php issue, I've moved the thread to PHP coding help.

The query is probably working fine. The problem is that you are returning multiple columns with the same name. For instance:

 

SELECT Table1.StateName, Table2.StateName FROM ... 

will return two columns and they both will be named "StateName". When the database access layer (i.e. mysql_fetch_assoc()) dumps this into a PHP array, one column overwrites the other. You cannot have two indexes in the same array with the same name. The column names will NOT be qualified by the table name in the PHP array.

 

So, you have two choices. Use mysql_fetch_array() to get the columns as a numeric array (without column names) or use an alias on some of the columns so they will have a different name in the array:

 

SELECT Table1.StateName, Table2.StateName AS OtherStateName ...

Thanks again Pikachu. This got it:

 

SELECT states_provinces_0.StateName AS StateName_1, states_provinces_0.StateID AS StateID_1, listings.*, states_provinces.StateID, states_provinces.StateName FROM ((listings LEFT JOIN states_provinces ON states_provinces.StateID=listings.location1provstate) LEFT JOIN states_provinces AS states_provinces_0 ON states_provinces_0.StateID=listings.location2provstate)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.