Re: PDO question: any way to resolve duplicate column names with PDO_FETCH_ASSOC?

From: Date: Thu, 19 May 2005 15:34:47 +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-2374@lists.php.net to get a copy of this message
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? --Wez. On 5/19/05, Matthew Peters <matthew_peters@uk.ibm.com> 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 and > step through the results, 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. > > (This is attempt 2 to post this - first attempt bounced as I was not in > pecl-dev@pair1.php.net's list - excuse me if this appears twice) > > -- > Matthew Peters > IBM Hursley Park > > -- > PECL development discussion Mailing List (http://pecl.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > >

« previous php.pecl.dev (#2374) next »