Re: PDO question: any way to resolve duplicate column names with PDO_FETCH_ASSOC?
| From: | Richard Thomas | Date: | Thu, 19 May 2005 15:45:18 +0000 |
| Subject: | Re: PDO question: any way to resolve duplicate column names with PDO_FETCH_ASSOC? | ||
| References: | 1 | Groups: | php.pecl.dev |
| Request: | Send a blank email to pecl-dev+get-2375@lists.php.net to get a copy of this message | ||
Your going to have this problem regardless, matter fact I thought that would cause a error in a database query? At least it does for MySQL. If 2 columns have matching names you get a error back and you have to specifiy which one you want
SELECT t1.id as t1id, t2.id FROM
If you need both values, not defining there output is asking for trouble.
In my opinion you should never build your scripts on PDO_FETCH_NUM because it means your building your script based on the numberic position in the array returned and changes you make to your database table in the future could break all code previously done by changing the order of results.
Richard Thomas - CEO
Cyberlot Technologies Group Inc.
507.398.4124 - Voice
Matthew Peters wrote:
I really like the PDO_FETCH_ASSOC behaviour where I get back an associative array indexed by column name, but I have a problem when there are two columns with the same name in the result set: for example when I join two tables that both contain a column called 'id', or 'name', for example. Experiment shows that one of them hides the other. I would really like it there a way to get both back and to know which was from which table. The simplest solution to my mind would be if there were a way to get the keys in the associative array to be qualified with table name: 'table1.id' and 'table2.id', for example. Does anyone know of a way of doing this or something similar? You might think I should just use the PDO_FETCH_BOTH or PDO_FETCH_NUM, but I am writing some tooling where I do not construct the SQL query in the first place but do need to inspect the result set and know which column is which. My only choices at the moment seem to be to either parse the SQL query or to get the user to tell me which columns are which in the result set. TIA