2014-04-01 32 views
-3
<?php 
       $con=mysqli_connect("","","",""); 
       if (mysqli_connect_errno()) 
        { 
       echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
        } 
        $result = mysqli_query($con,"SELECT * FROM itiraf"); 
        $result2 = mysqli_query($con,"SELECT oy FROM users WHERE username='". $Account->session('display_name') ."'"); 
       if(mysqli_num_rows($result)=="0"){echo "Henüz itiraf yapılmamış";} 
        else{ 
        echo "<table border='1' class='imagetable'> 
        <tr> 
        <th>NO:</th> 
        <th>İsim</th> 
        <th>İtiraf</th> 
        <th>Oy</th> 
        <th>Oy Kullan</th> 
        </tr>"; 

        while($row = mysqli_fetch_array($result)){ 
        echo "<tr>"; 
        echo "<td><b>" . $row['id'] . "</b></td>"; 
        echo "<td><b>" . $row['username'] . "</b></td>"; 
        echo "<td><b>" . $row['itiraf'] . "</b><br></td>"; 
        echo "<td><b>" . $row['oy'] . "</b><br></th>"; 

        while($row2 = mysqli_fetch_array($result2)){ 
         if ($row2['oy'] ==10) { echo "<td>Oy kullandınız</td>";} 
           else{echo "<td><a href='oy.php?id={$row[id]}'><img src='assets/images/up.png'/></a></td>";}} 

        echo "</tr>"; 
        echo "</form>";} 
        echo "</table>"; } 
       mysqli_close($con);?> 

第一次工作。第二,雖然工作,但不是很好。只有一次,我可以看到豎起大拇指的形象。我在這裏添加了關於表格的所有代碼。任何想法?雖然裏面有2個結果

+2

請格式化您的代碼 – Typoheads

+0

^+1,將很高興知道你想達到什麼,以及有什麼問題。 –

+0

在打印「無法連接到MySQL」(與嘗試在下一行執行查詢)之後,您會發現停止執行或者有不同的控制流來處理場景很有用。 – faintsignal

回答

0

在僞代碼,它附加:

{ //first iteration of while($row = mysqli_fetch_array($result)) 
    ... 
    { //first iteration of while($row2 = mysqli_fetch_array($result2)) 

    } 
    { //Second iteration of while($row2 = mysqli_fetch_array($result2)) 

    } 
    ... 
    { //last iteration of while($row2 = mysqli_fetch_array($result2)) 

    } 
    // know, $result2 is empty 

} 
{//Second iteration of while($row = mysqli_fetch_array($result)) 
    ... 
    //Here, $result2 is empty, so the second while loop doesn't work 
} 
如果你想重用$ RESULT2

,你可以之前將其存儲在一個數組,然後在while循環

0

你的第二個使用此陣while循環迭代存儲在$result2.
中的查詢結果因此,第一次完全執行第二個while循環時,光標移動到表的末尾而不回到第一個位置。
一種解決方案是將第二個查詢(其結果在$result2)中的所有值存儲在另一個數組中,然後在第二個while循環中使用該數組。
另一種解決方案是將第二個查詢本身在第一個while循環中移動,但這意味着查詢將每次執行。