Array Association

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rainman

    #1

    Array Association

    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_ass oc() 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.

    Mark
  • Chung Leong

    #2
    Re: Array Association

    Rainman wrote:[color=blue]
    > 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.[/color]

    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.

    Comment

    • Magzilla

      #3
      Re: Array Association

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

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

      And after your call to $sth=mysql_fetc h_assoc(),
      You could use $sth["Aname"] to reference to tableA.name. And
      $sth["Bname"] to reference to tableB.name.

      Comment

      Working...