dongtuo6562 2015-03-22 12:31
浏览 24
已采纳

如何回显另一个表中的值?

Hello I have 2 tables named tbl_guard and tbl_records. The tbl_guard has columns guard_id and fullname, while tbl_records has guard_id also. And here is my code:

$query = "SELECT * FROM tbl_records";
$stmt = $dbc->prepare($query);
$stmt->execute();

while($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
    extract($row);
    echo "<table>";
    echo "<tr>";
    echo "<td>Name</td>";
    echo "</tr>";
    echo "<tr>";
    echo "{$guard_id}";
    echo "</tr>";
}
echo "</table>";

What I want to do is instead of the guard_id being echoed there in the loop, I want the fullname in the tbl_guard table.

Your help would be very much appreciated!

  • 写回答

3条回答 默认 最新

  • douhan1860 2015-03-22 13:37
    关注

    Use JOIN

    $query = "SELECT tbl_records.*, tbl_guard.fullname FROM tbl_records LEFT 
    JOIN tbl_guard ON tbl_guard.guard_id = tbl_records.guard_id"; 
    

    In PHP

    while($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
    .
    .
    .
     echo "{$fullname}";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器