tags:

views:

25

answers:

2

Hello, If I am joining two tables and the result set will have ambiguous coloumn names, how can I retrieve the correct one through php?

Example:

$results = mysql_query("SELECT b.name, m.name FROM Brands as b INNER JOIN Models as m ON b.id = m.brand_id", $connection);

while ($row = mysql_fetch_array($results)) {
  printf("Name: %s", $row["name"]);
}

How can I get access to b.name and m.name from the $row array?

Thank you.

+4  A: 

Alias it in your MySQL query with AS. For example: SELECT name AS display_name, name FROM users;

It's not in PHP, but that's the common way of doing it.

Jason McCreary
+2  A: 

set an alias to each field using as

    $results = mysql_query("SELECT b.name as bname, m.name as mname FROM Brands as b INNER JOIN Models as m ON b.id = m.brand_id", $connection);

    while ($row = mysql_fetch_array($results)) {
  printf("Name: %s", $row["bname"]);

}
Treby
is there any other way? I find this kind of messy? something built into php perhaps?
phpnewbie23