开发者

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", $conn开发者_StackOverflow中文版ection);

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

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜