Same Fields Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
emrezen
Forum Newbie
Posts: 3
Joined: Tue May 27, 2008 6:27 am

Same Fields Problem

Post by emrezen »

I'm having a problem while i'm getting values of `ID` fields from 2 tables in one query. I have same fields as "ID" but these are at different tables.

Tables:
-----------------------------------------
users
--ID
.
.
.

estate_properties
--ID
.
.
.

$sql = mysql_query(select * from users,estate_properties where ...)
while($row = mysql_fetch_assoc($sql)) {
echo $row[ID]; //--> which ID
}

is that possible to use $row[user.ID] or anything else?
User avatar
N1gel
Forum Commoner
Posts: 95
Joined: Sun Apr 30, 2006 12:01 pm

Re: Same Fields Problem

Post by N1gel »

You could use mysql_fetch_row and reference the desired column as the number in the $row array.

Code: Select all

 
$sql = mysql_query(select users.ID,estate_properties.ID from users,estate_properties where ...)
while($row = mysql_fetch_row($sql)) 
{
    echo $row[0]; //--> users.ID
    echo $row[1]; //--> estate_properties.ID
}
 
emrezen
Forum Newbie
Posts: 3
Joined: Tue May 27, 2008 6:27 am

Re: Same Fields Problem

Post by emrezen »

thanks but is there any uses of mysql_fetch_assoc?
User avatar
N1gel
Forum Commoner
Posts: 95
Joined: Sun Apr 30, 2006 12:01 pm

Re: Same Fields Problem

Post by N1gel »

I don't think there are. I've Just read the following in the PHP Manual at mysql_fetch_assoc
If two or more columns of the result have the same field names, the last column will take precedence. To access the other column(s) of the same name, you either need to access the result with numeric indices by using mysql_fetch_row() or add alias names. See the example at the mysql_fetch_array() description about aliases.
emrezen
Forum Newbie
Posts: 3
Joined: Tue May 27, 2008 6:27 am

Re: Same Fields Problem

Post by emrezen »

thanks again
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Same Fields Problem

Post by Eran »

simply assign an alias to one of them, for example estate_properties.id AS estate_id
and then you could have both in the associative array
Post Reply