Jump to content


Prefixed field names

  • Please log in to reply
2 replies to this topic

#1 nfvindaloo

  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 17 March 2006 - 11:05 PM


I need to retrieve columns from the same table twice, using aliased names in the query.

When php retrieves a record i would expect ambiguous column names to be prefixed with the table name; ie: table.column, but mysql_fetch_assoc() seems to overwrite the ambigous columns so i only get the data from the last alias of the table.

There must be a way around this, is there a config option / runtime setting?


#2 fenway

  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 19 March 2006 - 07:53 PM

Well, there isn't a way to do this in MySQL -- if you ask for 2 fields with the same name, you'll get back 2 fields with the same name, and the 2nd one will overwrite the 1st in the hash. Of course, if you get back an array of columns, this isn't a problem. If you want table.column, ask for table.column. The DB doesn't handle this for you. However, it is possible that PHP has a wrapper for this that I'm not aware of.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 wickning1

  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 20 March 2006 - 06:11 AM

PHP doesn't have anything special for this. Use mysql_fetch_row() and get the results numerically. For instance:

SELECT col1, col2, col3, col1 FROM table
$row = mysql_fetch_row()
$col1_firsttime = $row[0];
$col2 = $row[1];
$col3 = $row[2];
$col1_secondtime = $row[3];

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users