PHP Mysqli 循环了太多结果


php mysqli looping too many results

>我在循环访问数据库检索结果时遇到了一个小问题,我理解为什么我会遇到这个问题,但不太确定如何实现解决方案,因为我的 PHP 不是最好的。我已经浏览了论坛,但找不到任何可以解决我的问题。

从下面开始,我的输出会给我 12 个结果,而我只想要 6 个结果,每个结果 2 个项目:每个循环在数组中计数 12 个项目,这是有意义的,所以它会输出 12 次,给我每个项目的重复项。 我如何最好地处理这个问题。

    MyTable1:                          myTable2:
    |   ID   |  Name  |           | NameID   |       Details     |   
    |--------|--------|           |----------|-------------------|
    |   0    | bob    |           |   0      | lives in the city |
    |   1    | david  |           |   1      | lives in a caravan|
    -------------------           --------------------------------

我的 MSQLI 查询:

     $qryRandom   =  "SELECT  MyTable1.ID,  MyTable2.Details
                      FROM  MyTable1
                      INNER JOIN  MyTable2
                      ON MyTable1.ID=MyTable2.NameID 
                      ORDER BY RAND()
                      LIMIT 6;";

我的PHP(一般示例)。

    $resultR= $con->query($qryRandom) or die($mysqli->error.__LINE__);
    while($row = mysqli_fetch_array($resultR, MYSQL_ASSOC))
    {
    foreach ( $row as $key=>$value ){ // loops 12 times here as there is 6 items x 2 values
    echo $key.": ".$value."<br>";
    }
 }

结果输出:

    bob lives in city
    bob lives in city
    David lives in caravan
    David lives in caravan
    john lives in the car 
    john lives in the car  // doubles of each entry is my issue
   // hope I was thorough and provided enough info for my scenario. 

我想出的解决方案是有效的,但我仍然不满意它;

   while($row = mysqli_fetch_array($resultR, MYSQL_ASSOC))
   {
    for ($i = 0; $i<1;$i++){
    echo $row['name'].$row['details']."<br>";
    }
    }

我更确定有更好的方法来解决这个问题。