tags:

views:

41

answers:

1

If i have this Query:

SELECT tableA.Id, tableB.Id FROM tableA JOIN tableB ON (tableA.bId = tableB.Id)

Now if i try this in php i have some problems:

while($result = mysql_fetch_array(mysql_query(/Query Above/)){
    print $result['tableB.Id'];
}

Now i know what you are going to say that i should do my query as:

SELECT tableA.Id AS aId, tableB.Id AS bId 
FROM tableA 
JOIN tableB ON (tableA.bId = tableB.Id)

and then use: $result['bId']

But the fact is the query is actually like this:

SELECT tableA.*, tableB.* FROM tableA JOIN tableB ON (tableA.bId = tableB.Id)

Is there any way i can get the result in PHP without doing the AS for each individual matching row?

Thanks

+4  A: 

You can't, per the PHP manual:

"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 either need to access the result with numeric indices by using mysql_fetch_row() or add alias names."

cf. http://www.php.net/manual/en/function.mysql-fetch-assoc.php

James McNellis
Ty, Atleast i know it cant be done.. cause my query is looking ugly now, and i was just hoping that there was a way :(. And i dont wana use index rows.. its all messy.. Thanks :D
Shahmir Javaid
You're welcome.
James McNellis
Another reason why you should never use the * wildcard in your queries ;-)
Duroth
Well the fact is... Im actually using all the info, There is not one going to waste :D
Shahmir Javaid