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

From: Date: Thu, 19 May 2005 18:25:14 +0000
Subject: Re: PDO question: any way to resolve duplicate column names with PDO_FETCH_ASSOC?
References: 1 2 3  Groups: php.pecl.dev 
Request: Send a blank email to pecl-dev+get-2378@lists.php.net to get a copy of this message
As Wez said "magically do the right thing". Regardless of what a database does and doesn't let you do, coding to rely on such behavior might cause some problems in the future because you have to depend on the returned order always being exactly the same. If another developer ever comes in and makes a change to the database they could break all your code very easy. Having columns with the same name I do not see as a problem, Using querys that "Need" both those columns in a specific order is where you run into issues. Its just good practice if you have to pull values from 2 diffrent tables that have matching columns to us table1.id as Whatever, table2.id as Whatever It will prevent possible issues in the future. I am not saying what he is going is "wrong" or "bad" just not "recommended" by most. Richard Thomas - CEO Cyberlot Technologies Group Inc. 507.398.4124 - Voice Dan Scott wrote:
DB2, for one, quite happily lets you return as many columns as you like with the same name. I believe this is standard SQL behaviour (gotta get my hands on one of those ANSI/ISO specs one of these days). Dan On 5/19/05, Richard Thomas <lists@cyberlot.net> wrote:
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
     
-- PECL development discussion Mailing List (http://pecl.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


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