Query result field name collision problem

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
hmsg
Forum Commoner
Posts: 42
Joined: Sun May 14, 2006 9:48 am

Query result field name collision problem

Post by hmsg »

Everah | Title changed to something a lot more descriptive

When i keep in a array a query made to a mysql DB, how can i use the content of it?

$var['table.field'] ? Is this the right way? I've tried this way but didn't work.

With the best regards

HMSG

[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:2. Use descriptive subjects when you start a new thread. Vague titles such as "Help!", "Why?" are misleading and keep you from receiving an answer to your question.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Check out the manual page for the mysql_fetch_array() function.

Cheers,
Kieran
hmsg
Forum Commoner
Posts: 42
Joined: Sun May 14, 2006 9:48 am

Post by hmsg »

I already saw, and i know how t use it if my select statment refer only to one table:

when i do $result=mysql_fetch_array($sql_select)

i can do $result['field_name'];

but when my sql_select refer for 2 tables? how can i call a field of a table and a field of the other table of the select statment?

$result['table1.field_name'] ..... $result['table2.fieldname']? it didn't work !


How can i call the $result mentioning the field that i want?
User avatar
boo_lolly
Forum Contributor
Posts: 154
Joined: Tue Nov 14, 2006 5:04 pm

Post by boo_lolly »

that's a good question. i'm not sure if there is a way to do it or not, but why don't you just have two different sql_fetch_array variables?

$result1 is selecting one table. $result2 is selecting another table. this way, you can call whichever one you want, wherever you want.

$result1['field_name'] ..... $result2['fieldname']?
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

You can use table1.field as x and table2.field as y and use x and y as array index.
Last edited by dibyendrah on Fri Dec 15, 2006 10:47 am, edited 1 time in total.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

AFAIK, there isn't a way, because you'll never need to do it. It's bad practice to be SELECT'ing 2 fields of the same name at one time anyway.

If your fields have different names, just reference them asif you are only querying one table. If your fields have the same name I think the variable will be overwritten, but don't quote me on that.

Why not use aliases in your SQL statement?

Code: Select all

SELECT 
  `employee`.`name` AS `employeename`, 
  `company`.`name` AS `companyname` 
FROM 
  `employee`, 
  `company`;

Code: Select all

$array['employeename']; $array['companyname'];
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Sorry - I misunderstood your question :?

jayshields is correct, you need to avoid the conflicts in your MySQL query with the use of "AS".

I think any fields that don't use "AS" will just be indexed by the local field name (no table name).

Cheers,
Kieran
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I thought this was mentioned in the manual. Since the array is built with all the resulting field names, if two field names collide, the second field name (and associated values) will be used. Seems logical.

To prevent that, do what jayshields suggested and alias your result field names in the query.

PS | And please use a more descriptive title. What is anyone supposed to do when they see 'Array' as the title?
Post Reply