Jump to content

Archived

This topic is now archived and is closed to further replies.

nfvindaloo

Prefixed field names

Recommended Posts

Hi

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?

Thanks,
Nic

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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];

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.