|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #56419 Please provide a way to resolve duplicate column names with PDO_FETCH_ASSOC
Submitted: 2005-06-20 12:34 UTC Modified: 2005-07-02 23:52 UTC
From: matthew_peters at uk dot ibm dot com Assigned: wez (profile)
Status: Closed Package: PDO (PECL)
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
40 - 15 = ?
Subscribe to this entry?

 [2005-06-20 12:34 UTC] matthew_peters at uk dot ibm dot com
This request summarises the exchanges that took place on between 13-19 June 2005 under the topic
"PDO question: any way to resolve duplicate column names with PDO_FETCH_ASSOC?"

Original request from Matthew Peters:
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 if there were 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: '' and '', for example. Does anyone know of a way of doing this or something similar?

Wez's reply:
Hi Matthew,

The problem is that not all of the underlying database libraries offer up information about the table names, so we can't guarantee that they'll be there.

For those that do have the info, we can add an attribute that you'd pass in when you prepare the query, that would cause the table names to be included in the keys.  For drivers that don't support that, an error would be raised.

I suspect that you're also looking for a workable alternative when the driver doesn't support it; one possible suggestion is have a fetch mode that allows multiple columns with the same name to come back; we could represent this in PHP script space something like this:

$row = array('price' => 100, 'id' => array(1 /* from first table */, 2 /* from second */));

You're still faced with the problem of having to figure out which version of id belongs to which table, but now you'd have a way to determine when you need to start poking around the SQL (and which names to look for), so that you can avoid doing it on every query.

How does that sound?

Final exchange: followup from Matthew with Wez's answer
Hi Matthew,

> What you propose when the underlying library supports returning table
> names - adding an attribute on the prepare - would be fine. I wondered why
> you wouldn't want to put it on the fetch, like PDO_FETCH_ASSOC_QUALIFIED,
> but maybe it's extra work to collect the information in the first place?

> names of the tables and columns. I would need to know that the values in the
> arrays were in some guaranteed order i.e. that you had copied them from the
> result in a certain order, so that at least I knew id[0] appeared before
> id[1] in the SQL statement.

This should always be the case, as PDO will walk the columns left-to-right.

Would you mind opening up a bug report for this issue?
Although I do flag mail for follow up, it tends to be mostly minor stuff, so will likely forget to check back on this; the bug report will group it together with other PDO stuff so that I see it when I'm next looking.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2005-07-02 23:52 UTC]
I've added PDO_FETCH_NAMED to CVS (grab the next 5.1 snapshot from to try it out).
The commit message ( demonstrates its use.

I've added two new attributes for including catalog/table name information in the column names, but no driver provides support for these attributes just yet.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 26 17:01:31 2024 UTC