views:

24

answers:

2

I have lot of MySQL tables with the same column name. So Im looking for PDO or SQL hack for SELECT * FROM more tables - which will return table names in result sets.

Example:

'SELECT * FROM table0, table1';

Where both tables has 'name' column. But FETCH_ASSOC result returns only one 'name' - the last one.

Result:

echo $result["name"];

Wanted result:

echo $result["table0.name"];
echo $result["table1.name"];
...

Note that

  1. I cannot rename DB columns to be unique
  2. I cannot manualy create alias for each columns (lot of tables/columns)
  3. I want name in result set not numbers like FETCH_NUM

Any ideas? Thanks!

+1  A: 

Hack doesn't exist, you have to create aliases.

Naktibalda
+1  A: 

You said that you don't want to alias all columns because there are too many, but have you considered only aliasing the ones that give you problems?

SELECT
    *, 
    table0.name AS t0name,
    table1.name AS t1name
FROM table0 JOIN table1 ON ...
Mark Byers
Thanks for partial suggestion. I have found another. FETCH_NAMED returns problematic columns as numbered array and other as ordinary scalars. [unique] => val[name] => Array( [0] => val [1] => val )One way is to format result set with php.
Cervenak