Php mysql内部while循环查询

Php mysql内部while循环查询,php,mysql,Php,Mysql,我有两张桌子。在表A中存储数据,在表B中存储相应表A的注释 表A结构 id content title likes comments_count id是自动递增的,内容是数据 表B结构为 id joke_id Description 对应数据的表B-U id和表A id相同 我使用此查询显示表A中的5行,并希望为每个表A数据显示3条注释,但它为所有5行显示与表B中表A id数据对应的相同内容。 我会举个例子让它更清楚 $res2 = $con->

我有两张桌子。在表A中存储数据,在表B中存储相应表A的注释

表A结构

id  content     title   likes   comments_count 
id是自动递增的,内容是数据

表B结构为

        id  joke_id Description 
对应数据的表B-U id和表A id相同

我使用此查询显示表A中的5行,并希望为每个表A数据显示3条注释,但它为所有5行显示与表B中表A id数据对应的相同内容。 我会举个例子让它更清楚

$res2 = $con->query("SELECT * FROM `Table-A` order by id desc limit 5");

while($row2 = $res2->fetch_assoc()){
    $likes = $row2['likes'];
    $comment = $row2['comments_count'];
        $cont= $row2['content'];

$idd=$row2['id'];

$res29 = $con->query("Select * from `Table-B` where joke_id='$idd' order by id desc limit 4");

while($row29 = $res29->fetch_assoc()){
            $contpp= "";

    $contpp .= '<li>'.$row29['description'].'<p></li>';


   }

 }




$output .='<div id="'.$idd.'"  align="left" class="messagelove_box" ><div class="content_box_1">
         <div class="content_box_3_text">'.nl2br($cont).'</div> 

        <div id="header'.$idd.'" class="content_box_31_text"><ul id="commentlist" class="justList">'.$contpp.'</ul></div>



</div></div>
';
  }
$res2=$con->query(“从`Table-A`中选择*order by id desc limit 5”);
而($row2=$res2->fetch_assoc()){
$likes=$row2['likes'];
$comment=$row2['comments_count'];
$cont=$row2['content'];
$idd=$row2['id'];
$res29=$con->query(“从'Table-B'中选择*,其中,'u id='$idd'按id顺序描述限制4”);
而($row29=$res29->fetch_assoc()){
$contpp=“”;
$contpp.='
  • 。$row29['description'].
  • ; } } $output.=' “.nl2br($cont)。”
      。$contpp.
    '; }

    但它为每行显示相同的前4条注释

    在表名中使用反勾号

     $res29 = $con->query("Select * from `Table-B` where joke_id='$idd' order by id desc limit 4");
    
    现场演示:

  • 无回勾:

  • 带回勾:


  • 同时使用
    $output=“”
    $output.=

    之前,您是否也使用了
    $contpp=“”
    $contpp.=
    之前?更新您的代码。放入
    $contpp=“”在while循环
    while($row29
    )之外。同时在while循环
    while($row2
    我刚才在表名中使用了backtick
    `
    )前面加上
    $output=“;