dongxu0690 2017-03-10 11:37
浏览 44
已采纳

使用JOIN时将MySQL值转换为PHP变量

I can get a value from the database and echo it out like so:

$sql = "SELECT id 
          FROM table1";

$result = $conn->query($sql);
$res = mysqli_fetch_assoc($result);
echo $res['id'];

Although, when using any sort of join an issue occurs

$sql = "SELECT table1.id,
               table2.id 
          FROM table1
          JOIN table2";

$result = $conn->query($sql);
$res = mysqli_fetch_assoc($result);
echo $res['id'];

I can't echo out the value like I previously could with a single table.

How do I echo out the id from a specific table, either table1 or table2? I tried echo $res['table1.id']; but that doesn't seem to work out

  • 写回答

1条回答 默认 最新

  • duandushang5148 2017-03-10 11:45
    关注
    $sql = "SELECT table1.id as id,
               table2.id as id1 
          FROM table1
          JOIN table2";
     $result = $conn->query($sql);
     $res = mysqli_fetch_assoc($result);
     echo $res['id1']."--".$res['id'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 spring后端vue前端
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题