Page 1 of 1

Query result field name collision problem

Posted: Fri Dec 15, 2006 10:01 am
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.

Posted: Fri Dec 15, 2006 10:19 am
by Kieran Huggins
Check out the manual page for the mysql_fetch_array() function.

Cheers,
Kieran

Posted: Fri Dec 15, 2006 10:25 am
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?

Posted: Fri Dec 15, 2006 10:37 am
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']?

Posted: Fri Dec 15, 2006 10:39 am
by dibyendrah
You can use table1.field as x and table2.field as y and use x and y as array index.

Posted: Fri Dec 15, 2006 10:42 am
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'];

Posted: Fri Dec 15, 2006 11:00 am
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

Posted: Fri Dec 15, 2006 2:53 pm
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?