Juan Martínez
Juan Martínez

Reputation: 51

Php associative array when joining tables with the same field names

I have this consult and I need the key names of the array I'm obtaining:

SELECT table_1.*, table_2.* FROM... INNER JOIN...

I checked other posts with similar titles and the solution is always to use an alias. This won't work for me because I have too many fields and it would be troublesome to write down every single one.

The problem is, of course, that the field names are the same in both tables.

It wouldn't be a problem if I could simply echo $arr["table_1.field_name"] but it doesn't work.

Yes, I could use the index, like $arr[0], $arr[1], $arr[2]. But if I change the table fields order or add new fields then I'll have to change it in the program and it does not seem like a proper solution either.

Thank you very much in advance.

Upvotes: 0

Views: 242

Answers (1)

delboy1978uk
delboy1978uk

Reputation: 12365

Use AS to alias the second column. It's always the solution, because it IS the solution!

SELECT table1.mycolumn, table2.mycolumn AS table2column

Upvotes: 1

Related Questions