2013-09-26 94 views
0

我有沒有工作的其他循環內循環,這是代碼:mysqli_fetch循環不工作

while($row = mysqli_fetch_array($result)) 
    { 
    echo "<tr>"; 
    echo "<td>" . $row['rowId'] . "</td>"; 
    echo "<td>" . $row['startDate'] . "</td>"; 
    echo "<td>" . $row['eventName'] . "</td>"; 
    echo "<td>" . $row['betName'] . "</td>"; 
    $string1 = "SELECT * FROM newCell WHERE rowId ='"; 
    $string2 = $row['rowId']."'"; 
    $result2 = $string1.$string2; 
    echo "<td>" . $result2 . "</td>"; 

    while($row2 = mysqli_fetch_array($result2)) 
    { 
     echo "<td>" . $row2['odds'] . "</td>"; 

     echo "<td>" . $row2['outcomeName'] . "</td>"; 
    } 
    echo "</tr>"; 
    } 

當我直接查詢$結果2到BBDD第一個結果就顯示了三個結果,但代碼不會進入第二個LOOP。爲什麼?這裏有任何錯誤?

+1

您不在任何地方執行第二個循環的查詢。 –

+0

告訴我們你的兩個查詢.. – Kalpit

+0

你要求它循環一個'string',而不是'mysqli'對象? – MackieeE

回答

0
while($row = mysqli_fetch_array($result)) 
    { 
    echo "<tr>"; 
    echo "<td>" . $row['rowId'] . "</td>"; 
    echo "<td>" . $row['startDate'] . "</td>"; 
    echo "<td>" . $row['eventName'] . "</td>"; 
    echo "<td>" . $row['betName'] . "</td>"; 
    $string1 = "SELECT * FROM newCell WHERE rowId ='"; 
    $string2 = $row['rowId']."'"; 
    $result2 = $string1.$string2; 
    echo "<td>" . $result2 . "</td>"; 

    $result2 = mysqli_query($connection, $result2); 

    while($row2 = mysqli_fetch_array($result2)) 
    { 
     echo "<td>" . $row2['odds'] . "</td>"; 

     echo "<td>" . $row2['outcomeName'] . "</td>"; 
    } 
    echo "</tr>"; 
    } 
0

用途:

$query = "SELECT ...."; 
$result2 = mysqli_query($db, $query); 

while($row2 = mysqli_fetch_array($result2)) 
{ 
     echo "<td>" . $row2['odds'] . "</td>"; 

     echo "<td>" . $row2['outcomeName'] . "</td>"; 
} 
0

我覺得$ RESULT2應該是mysqli_query不僅僅只是查詢輸出。與MySQL類似。

也許你應該有這樣的事情

$result2 = mysqli_query($result2); 
0

嘗試這個

<?php 
while($row = mysqli_fetch_array($result)) 
    { 
    echo "<tr>"; 
    echo "<td>" . $row['rowId'] . "</td>"; 
    echo "<td>" . $row['startDate'] . "</td>"; 
    echo "<td>" . $row['eventName'] . "</td>"; 
    echo "<td>" . $row['betName'] . "</td>"; 
    $string1 = "SELECT * FROM newCell WHERE rowId ='"; 
    $string2 = $row['rowId']."'"; 
    $result2 = $string1.$string2; 
    echo "<td>" . $result2 . "</td>"; 
    $results = mysqli_query($db,$result2); 
    while($row2 = mysqli_fetch_array($results)) 
    { 
     echo "<td>" . $row2['odds'] . "</td>"; 

     echo "<td>" . $row['outcomeName'] . "</td>"; 
    } 
    echo "</tr>"; 
    } 
    ?> 
0

之前獲取數據使用mysqli_query()函數,然後運行mysqli_fetch_array(執行MySQL查詢)。如果您將結果計數爲$ count = mysqli_num_rows($ query),並用if ... else管理您的代碼,那就太好了。