2013-11-02 60 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