简体   繁体   中英

PHP MYSQL: How to get ambiguous column result from joined table

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.

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.

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"]);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM