As mentioned above, I am trying to assign an alias to all columns of a table at once as there is an obvious problem in PDO if a column name is used in more than one table.
If I'm trying this SQL statement:
SELECT t12.*,t1.*,t2.* FROM `tableone2tabletwo` t12
LEFT JOIN tableone t1 ON t12.idone=t1.id
LEFT JOIN tabletwo t2 ON t12.idtwo=t2.id
I'll receive an array like this with$stmt->fetch()
:
Array
(
[id] => 2
[0] => 1
[idone] => 1
[1] => 1
[idtwo] => 2
[2] => 2
[3] => 1
[name] => Test2TwoText
[4] => Test1Text
[5] => 2
[6] => Test2TwoText
)
As you see, the field gets overwritten(the last time by the last element with this name... ;-)), even though the table has an alias.
fetch(PDO::FETCH_ASSOC)
as Andrii Filenko recommended returns this:
Array
(
[id] => 2
[idone] => 1
[idtwo] => 2
[name] => Test2TwoText
)
So here the data is even lost, not only just available via the numeric indices.
fetch(PDO::FETCH_NAMED)
as Nigel Ren recommended returns this:
Array
(
[id] => Array
(
[0] => 1
[1] => 1
[2] => 2
)
[idone] => 1
[idtwo] => 2
[name] => Array
(
[0] => Test1Text
[1] => Test2TwoText
)
)
Is there a simple and stylish solution for this problem or will I have to do this one by one?
(My DB is available at DB-fiddle if you want to take a look!)