php 如何使用PHP从具有多个同名列的MySQL行中获取结果?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1416980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fetch result from MySQL row with multiple same-name columns with PHP?
提问by omg
select * from A left join B on A.columnc=B.columnd
results returned by above SQL will include both columns of A and B.
上述 SQL 返回的结果将包括 A 和 B 列。
And what if A and B have some columns with the same name?
如果 A 和 B 有一些同名的列怎么办?
How to retrieve the value from PHP?
如何从 PHP 检索值?
回答by Aron Rotteveel
You probably want to be more explicit in your query. That way you can provide aliases for your columns:
您可能希望在查询中更加明确。这样你就可以为你的列提供别名:
SELECT
A.foo as a_foo,
B.foo as b_foo
FROM A
LEFT JOIN B ON A.columnc = B.columnd
回答by quosoo
The answer is actualy in the PHP documentation:
答案实际上在PHP 文档中:
"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. "
“如果结果的两列或更多列具有相同的字段名称,则最后一列优先。要访问同名的其他列,您需要通过使用mysql_fetch_row()或添加别名来访问带有数字索引的结果名称。请参阅mysql_fetch_array()有关别名的说明中的示例。”
Especialy mysql_fetch_array()seems to be the best candidate when you insist on using star in the select statement:
mysql_fetch_array()当你坚持在 select 语句中使用 star 时,Especialy似乎是最好的候选人:
$row = mysql_fetch_array($result, MYSQL_BOTH)
Then you can refer to the unambigous fields by $row[name]and to the ambigous one by $row[col_number], but that limits portability of your code (maybe next version of MySQL is going to return columns in a different order?). Recommended solution is to rewrite your query and list all the required fields instead of using star and for the ambigous ones - use aliases.
然后你可以引用明确的字段 by$row[name]和不明确的字段 by $row[col_number],但这限制了你的代码的可移植性(也许下一版本的 MySQL 会以不同的顺序返回列?)。推荐的解决方案是重写您的查询并列出所有必填字段,而不是使用星号,对于不明确的字段 - 使用别名。
回答by Don G.
In Java+MySQL from the ResultSet object, you can use for example getString("a.id") and also getString("b.id"), if your query was like "SELECT a.id, b.id FROM a,b"
在来自 ResultSet 对象的 Java+MySQL 中,您可以使用例如 getString("a.id") 和 getString("b.id"),如果您的查询类似于“SELECT a.id, b.id FROM a,乙”
I dunno if there is something like this in PHP.
我不知道 PHP 中是否有这样的东西。
Regards
问候
回答by Eimantas
You should use column aliasesin the select statement.
您应该在 select 语句中使用列别名。
回答by Jordan Edwards
This may be useful to somebody: Because I was using some templating, I couldn't easily use aliases each time, and I wanted the associative array for ease of use. CREDIT PILCROW for his answer on how to mysql_fetch_array on joined tables, but columns have same name, but in MySQLi:
这可能对某些人有用:因为我使用了一些模板,所以我不能每次都轻松地使用别名,我希望关联数组易于使用。CREDIT PILCROW 关于如何在连接表上使用 mysql_fetch_array 的回答,但列具有相同的名称,但在 MySQLi 中:
$qualified_names = array();
for ($i = 0; $i < mysqli_num_fields($result); ++$i) {
$fieldinfo=mysqli_fetch_field_direct($result,$i);
$table = $fieldinfo->table;
$field = $fieldinfo->name;
$qualified_names["$table.$field"]="$table.$field";
}
$newrow = array_combine($qualified_names, mysqli_fetch_array($result,MYSQLI_NUM));

