You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP实现MySQL双表关联展示:table1下显示对应table2数据问题

问题描述

我创建了两个MySQL表:

  • table1:包含id、codes、titles字段
  • table2:包含id、table1_id、column1、column2、column3字段

需求是:向table1插入codes、titles数据,向table2插入column1、column2、column3数据后,用PHP展示时,每条table1的数据下方显示与之关联的table2数据。但运行代码后展示效果不符合预期——预期是每条table1条目下只显示对应关联的table2数据,实际是所有table2数据都重复出现在每个table1条目下方。

原代码如下:

<?php   
include('config.php');
$ret=" SELECT * from table1
";
$stmt= $mysqli->prepare($ret);
//$stmt->bind_param('i',$aid);
$stmt->execute() ;//ok
$res=$stmt->get_result();
$cnt=1;
while($row=$res->fetch_object())
{
?>

 <div class="card shadow">
 <div class="card-header py-0">
 <table class="table">
 <tr >
 <td style="border: none"><?php echo $row->codes; ?></td>
 <td style="border: none"><?php echo $row->titles; ?></td>
 <td style="border: none">edit</td>
 </tr>
 </table>
 </div>
<div class="card-body">
                       
<div class="table-responsive table mt-2" id="dataTable" role="grid" aria-describedby="dataTable_info">
<table class="table my-0" id="dataTable">
<thead>
                              
<tr>
<th>column 1</th>
<th>column 2</th>
<th> column 3 </th>
                                       

</tr>
</thead>
<tbody>
                                <?php  



$ret=" SELECT * from table2";
$stmt= $mysqli->prepare($ret);

$stmt->execute();
 $res=$stmt->get_result();
 while($row=$res->fetch_object()    )
 {

 ?>                                 
 <tr>
 <td><?php echo $row->column1 ?></td>
 <td> <?php echo $row->column3 ?></td>
 <td> <?php echo $row->column3 ?></td>
</tr>

 <?php } ?>                           
</tbody>
<tfoot>
<tr></tr>
</tfoot>
</table>
</div>




 <h3 class="text-dark mb-4"> <button class="btn btn-info " data-toggle="modal" data-target="#login_itech3">adding column</button></h3>
                    </div>
                </div>



<br>
<?php } ?>
问题原因与修复方案

问题根源

当前代码在循环table1的每条数据时,查询table2的语句是SELECT * from table2,没有添加关联条件,导致每次都查询出所有table2的数据,所以每个table1卡片下都显示全部table2内容;另外还存在字段输出错误——把column2重复输出成了column3。

修复后的完整代码

<?php   
include('config.php');
$ret = "SELECT * from table1";
$stmt = $mysqli->prepare($ret);
$stmt->execute();
$res = $stmt->get_result();

while($row_table1 = $res->fetch_object())
{
?>
 <div class="card shadow">
 <div class="card-header py-0">
 <table class="table">
 <tr >
 <td style="border: none"><?php echo $row_table1->codes; ?></td>
 <td style="border: none"><?php echo $row_table1->titles; ?></td>
 <td style="border: none">edit</td>
 </tr>
 </table>
 </div>
<div class="card-body">
                       
<div class="table-responsive table mt-2" id="dataTable_<?php echo $row_table1->id; ?>" role="grid" aria-describedby="dataTable_info">
<table class="table my-0" id="dataTable_<?php echo $row_table1->id; ?>">
<thead>
                              
<tr>
<th>column 1</th>
<th>column 2</th>
<th> column 3 </th>
                                       
</tr>
</thead>
<tbody>
                                <?php  
// 查询当前table1条目关联的table2数据
$ret_table2 = "SELECT * from table2 WHERE table1_id = ?";
$stmt_table2 = $mysqli->prepare($ret_table2);
// 绑定table1的id参数,避免SQL注入
$stmt_table2->bind_param('i', $row_table1->id);
$stmt_table2->execute();
$res_table2 = $stmt_table2->get_result();
while($row_table2 = $res_table2->fetch_object())
{
?>                                 
 <tr>
 <td><?php echo $row_table2->column1 ?></td>
 <td><?php echo $row_table2->column2 ?></td> <!-- 修正原代码的字段输出错误 -->
 <td><?php echo $row_table2->column3 ?></td>
</tr>

 <?php } ?>                           
</tbody>
<tfoot>
<tr></tr>
</tfoot>
</table>
</div>

 <h3 class="text-dark mb-4"> <button class="btn btn-info " data-toggle="modal" data-target="#login_itech3">adding column</button></h3>
                    </div>
                </div>

<br>
<?php } ?>

关键修复点说明

  1. 添加关联查询条件:查询table2时通过WHERE table1_id = ?绑定当前table1的id,确保只查询对应关联的数据
  2. 参数绑定:使用bind_param传递table1的id,避免SQL注入风险
  3. 修正字段输出:把原代码中重复输出的column3改成column2
  4. 修复DOM重复id:将表格id改为dataTable_<?php echo $row_table1->id; ?>,避免页面中出现重复id的DOM元素

性能优化建议

可以通过一次关联查询获取所有数据,再在PHP中分组处理,减少数据库查询次数:

SELECT t1.*, t2.column1, t2.column2, t2.column3 
FROM table1 t1 
LEFT JOIN table2 t2 ON t1.id = t2.table1_id

查询后将结果按table1的id分组,再循环展示,适合数据量较大的场景。

内容的提问来源于stack exchange,提问作者Beniamin Ahmadi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 09:54:25