0

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.

5
  • when you fetch into an assoc array you can get the keys of that array, which are the columns then. This way you could keep it dynamic. Commented Oct 25, 2018 at 15:10
  • I have too many fields Too many fields is symptomatic of poor design and/or laziness. Commented Oct 25, 2018 at 15:12
  • So what are the keys you get right now? Commented Oct 25, 2018 at 15:12
  • or why don't you simply foreach the $row? Then you see what the columns are called, and you get the values straight out of it. Commented Oct 25, 2018 at 15:16
  • I don't get the ID field because it has the same name on every table. So, my first ID is on index [0] but the next one would be on the index [100], to say something. If I add more fields to the first table then I'll have to change that index [100] for another number. What if it wasn't me but another programmer? We can't update a program after every database change. Commented Oct 25, 2018 at 15:44

1 Answer 1

1

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

SELECT table1.mycolumn, table2.mycolumn AS table2column
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer but I really need to select all the fields, like: SELECT table_1.*, table_2.* ...

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.