2

I'm running a query in php/mysql that joins several tables and I'm trying to display the results on my page. My problem is that all tables have the same field names, leading to an abgitious name problem when I try to display the content as:

echo $row_result['name']; // this would be i.e. the name of the product but I also have another table 'descriptions' in which I also have a field 'name'

I tried echoing $row_result['table_name.field_name'] but this won't work. Is there another way, apart from using select description.name as prodDescription etc? Hope you can make sence of the above, I wrote it in a hurry!

1
  • You can always try a print_r on the returned array to see what values are being returned. If you only have 1 name value returned, then chances are the latter is overwriting the first and your only option, as Chris pointed out, is using the AS keyword or running two separate queries (the AS keyword is a far better choice). Any particular reason you don't want to use the AS keyword? As that is kind of the whole point of it... Commented Mar 23, 2011 at 15:24

1 Answer 1

7

Use the AS keyword. Like this:

SELECT A.column AS A_col, B.column AS B_col FROM A JOIN B ON A.key = B.key

Then you would just reference A_col and B_col

Sign up to request clarification or add additional context in comments.

6 Comments

@chris bikey77 already told he doesnt want to use this method
I was hoping that there is another way because I'd have to write a large amount of AS statements and I need to do the same in several parts of my website.
The only other unambiguous way I'm aware of is to access the columns by their numerical position (i.e. $row_result[0]).
@bikey77, Well there are not many choice. A: name the 'descriptions' table 'name' to prodDescription, which would require editing any place that column is hardcoded as 'name'. B: Change your code to Alias it out. Either or you are looking at changing code in one form or the other. Which is better, is up to you. @Chris, does the numerical indexed array pull out both sets?
To answer my own question, yes a mysql_fetch_array or mysql_fetch_row will pull out both items using the numerical index as Chris suggested. :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.