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 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化