2013-11-02 61 views
0

我在循环查看数据库检索结果时遇到了一些小问题,我明白为什么我要解决这个问题,但不太清楚如何实施解决方案,因为我的PHP不是最好的。 我已浏览论坛,但无法找到任何解决我的问题。php mysqli循环太多结果

从下面我的输出会给我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(generall例子)。

$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. 
+0

您的“一般示例”与详细描述相矛盾。这使整个问题不是真正的问题 –

+0

您提供的查询无法产生此类输出。 –

回答

1

我想出了作品我怎么过我仍然不喜欢它的解决方案;

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

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

+0

为什么要投票? – Careen