dsf12123 2016-11-19 05:27
浏览 91
已采纳

PHP mySQL表比较两列

I need to compare data between two tables.

t1 = playerachievements

| ID | Username | Achievement ID |

t2 = achivements

| ID | Achievement Name | Image Location |

Now I search for items in t1 which match the username of the logged in user

$sql = "SELECT * FROM playerachievement WHERE username = '$user'";

Now what I need to do is grab the Image Location for each achievement the user has based on the Achievement ID in t1 and the ID in t2

What would be the best way to go about this?

EDIT:

I think I almost have it, however this only brings back one result:

$sql = "SELECT * FROM playerachievement WHERE username = '$user'";

$result = $db_conn->query($sql);
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $achid = $row['achievement'];
    }

    $sql = "SELECT * FROM achievements WHERE id = '$achid'";
    $result = $db_conn->query($sql);
    if ($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
                echo "<div class='custom-container'>
                        <img src='".$row["image"]."'</img></div>";
            }
    }
}
  • 写回答

1条回答 默认 最新

  • duanliaouu965826 2016-11-19 05:51
    关注

    for checking for each value you should give the condition inside while loop

    $sql1 = "SELECT * FROM playerachievement WHERE username = '$user'";
    $result1 = $db_conn->query($sql1);
    if ($result1->num_rows > 0) {
    // output data of each row
     while($row1 = $result1->fetch_assoc()) {
        $achid = $row1['achievement'];
    
      $sql2 = "SELECT * FROM achievements WHERE id = '$achid'";
      $result2 = $db_conn->query($sql2);
      if ($result2->num_rows > 0) {
              while($row2 = $result2->fetch_assoc()) {
                  echo "<div class='custom-container'>
                          <img src='".$row2["image"]."'</img></div>";
              }
      }
     }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)