从php中的mysqli联接查询访问行 [英] Accessing rows from a mysqli join query in php

查看:77
本文介绍了从php中的mysqli联接查询访问行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

// db connection info set up earlier
$sql= "SELECT `TABLE_1.ID`, `TABLE_2.ID`, `POTATO` FROM `TABLE_1.ID` LEFT JOIN `TABLE_2` ON `TABLE_1`.`ID` = `TABLE_2`.`ID_OF_OTHER_TABLE`;";
$rows = mysqli_query($connection, $sql);
foreach ($rows as $row){
    $potato = $row["POTATO"];
    $id = $row["TABLE_2.ID"];
}

我无法获得TABLE_2.ID.我试图做一个print_r以获得正确的格式,但是它说这是一个mysqli对象,我没有比这更多的信息.但是,我可以拿土豆.因此,我猜测这是一个调用语法问题,但是我搜索了多个站点(包括堆栈和Google),但没有找到明确的答案.所以,我需要做什么而不是

I can't get TABLE_2.ID. I've tried to doing a print_r to get the proper format, but it says it's a mysqli object and I don't get much more info than that. However, I can get potato. So I'm guessing it's a calling syntax issue, but I've searched multiple sites (stack and google included) and not found a clear answer. So, what do I need to do instead of

$id = $row["TABLE_2.ID"];

?

推荐答案

将别名分配给具有相同名称的列.

Assign aliases to the columns with the same name.

$sql= "SELECT `TABLE_1`.`ID` AS t1_id, `TABLE_2`.`ID` AS t2_id, `POTATO` 
       FROM `TABLE_1.ID` 
       LEFT JOIN `TABLE_2` ON `TABLE_2`.`ID_OF_OTHER_TABLE` = `TABLE_1`.`ID`;";
$rows = mysqli_query($connection, $sql);
foreach ($rows as $row){
    $potato = $row["POTATO"];
    $id = $row["t2_id"];
}

这篇关于从php中的mysqli联接查询访问行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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