Page 1 of 1

mysql_fetch_assoc() question regarding field names

Posted: Fri Mar 12, 2010 12:51 pm
by ChrisF79
Greetings:

I have a very large query that pulls using some joins from 3 or 4 tables. The output is then grabbed through mysql_fetch_assoc(). When I'm pulling my data in the query, I'm using table aliases like l.id, l.bedrooms, l.street FROM listings AS l

By using this, I figured I could output the data as $data['l.street'] but it doesn't work. $data['street'] works just fine.

My question is, if I have two tables with the same field name, how do I distinguish them in the output?

Re: mysql_fetch_assoc() question regarding field names

Posted: Fri Mar 12, 2010 1:13 pm
by AbraCadaver
You'll either have to use mysql_fetch_array() and use the numeric index or alias the column name:

Code: Select all

SELECT l.id `l.id`, l.bedrooms `l.bedrooms`, l.street `l.street` FROM listings AS l
-or-

Code: Select all

SELECT `l`.`id` `l.id`, `l`.`bedrooms` `l.bedrooms`, `l`.`street` `l.street` FROM listings AS `l`
-or-

Code: Select all

SELECT l.id AS `l.id`, l.bedrooms AS `l.bedrooms`, l.street AS `l.street` FROM listings AS l
etc...

Re: mysql_fetch_assoc() question regarding field names

Posted: Fri Mar 12, 2010 1:15 pm
by requinix
I'd say to use aliases. If you use indexes, if the query is ever modified you might have to make a number of other changes.