mysql_fetch_assoc() question regarding field names

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ChrisF79
Forum Commoner
Posts: 26
Joined: Tue Apr 01, 2008 8:26 pm

mysql_fetch_assoc() question regarding field names

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: mysql_fetch_assoc() question regarding field names

Post 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...
Last edited by AbraCadaver on Fri Mar 12, 2010 1:16 pm, edited 1 time in total.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: mysql_fetch_assoc() question regarding field names

Post 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.
Post Reply