Array Association

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
In my query, I make a couple of INNER JOINS to tables which happen to  
have some fields of the same column name.  I use "SELECT * ..." and so  
numerous columns are returned from the query.  The problem is that when  
I use mysql_fetch_assoc() function, data in the columns of the same name  
overwrite each other in the array that's returned.

Is there a non-tedious way to get around this?  The tedious way is to  
list each column name in the SELECT list (instead of *) using "AS" to  
rename appropriate fields, but with so many columns returned, this will  
be a headache, especially since these tables are still evolving.


Re: Array Association

Rainman wrote:
Quoted text here. Click to load it

Nope, there is no method other than using column aliases that I know
of. It's not a terribly good practice to use SELECT * in a program in
any event, since you would end up retrieving columns that won't be
used. For instance, when you join two tables, you obviously wouldn't
need to fetch both the primary and the foreign key.

Re: Array Association

Another possible method to solve your problem. I demo it with following

In case you have two tables tableA and table B, both of them have name,
id as table fields.
In stead of using "SELECT, FROM tableA, tableB
  You could use: "SELECT AS Aname, AS Bname
FROM tableA, tableB WHERE",

And after your call to $sth=mysql_fetch_assoc(),
  You could use $sth["Aname"] to reference to And
$sth["Bname"] to reference to

Site Timeline