如何在联接的表上使用mysql_fetch_array,但是列具有相同的名称 [英] how to mysql_fetch_array on joined tables, but columns have same name

查看:62
本文介绍了如何在联接的表上使用mysql_fetch_array,但是列具有相同的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何获取结果从MySQL行开始,有多个同名的PHP列?

Possible Duplicate:
How to fetch result from MySQL row with multiple same-name columns with PHP?

我有两个表,它们共享相似的列名.

I have two tables, and they share similar column names.

查询是:

SELECT a.name,b.name
FROM tablea a 
JOIN tableb b ON a.id = b.id

结果放入数组:

while ($row = mysql_fetch_array($results)){
   $aname = $row['name'];
}

一旦我在第二张表中添加了内容,我注意到$aname正在使用tableb的数据.

Once I added in that second table I noticed the $aname was using tableb's data.

问题:我如何存储两个name列,$row['a.name']不起作用.我的猜测可能是我需要对查询中的每个结果进行别名化.有什么建议? 我是否应该避免将来再提供列名?

Question(s): How can I store both name columns, $row['a.name'] does not work. My guess is maybe I need to alias each result in the query. Any suggestions? Should I same avoid giving the column names in the future?


I know mysql_* is deprecated. Save your energy.

推荐答案

您的猜测是正确的.您需要为各列创建一个ALIAS,以便您可以唯一地获取它,

Your guess was right. You need to create an ALIAS for the columns so you can fetch it uniquely,

SELECT a.name Name1, b.name Name2
FROM tablea a 
JOIN tableb b ON a.id = b.id

然后您可以拨打电话

$row['Name1']

这篇关于如何在联接的表上使用mysql_fetch_array,但是列具有相同的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆