while循环始终打印最后插入的行结果

时间:2014-03-12 03:58:53

标签: php mysql if-statement while-loop

问题是while循环始终显示最后插入的行结果。我已经尝试过以下代码来关注/取消关注按钮选项。例如,我是用户ID = 1。我已经关注了用户id = 4。现在,我想关注用户id = 5。当我点击跟随按钮(id = 5)时,它会正确地变为取消关注。但是,我已经关注了用户id = 4。这变成了跟随。这是我的问题。

然后我尝试echo $following;。它打印5555.这意味着最后插入的数据。但是我想要45.我确定我在我的while循环中犯了一个错误。但我不知道应该改变什么?

        <?php
                try
                {
                    $stmt = $conn->prepare("SELECT * FROM users ORDER BY Autoid");
                    $stmt->errorInfo();
                    $stmt->execute();

                    $sth = $conn->prepare("SELECT * FROM followers ORDER BY Autoid");
                    $sth->errorInfo();
                    $sth->execute();
                    while($follow_row = $sth->fetch(PDO::FETCH_ASSOC))
                   {
                        $following = $follow_row['Following'];
                        $follower = $follow_row['Follower'];
                   }

                    while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
                       {
                        echo "<tr>";
                        echo "<td>". $row['Autoid'] ."</td>";
                        echo "<td>". $row['Name'] ."</td>";

                        // echo $row['Following'];
                        if($_SESSION['sesuname'] == $row['Username'])
                         {
                            echo "<td class='itsyou' >Its You ". $_SESSION['sesuname'] ."</td>";
                         }
                        else
                         {  
                            if(($follower == $_SESSION['sesid']) AND ($following != $row['Autoid']))
                            {
                               //echo "<td>true</td>";
                             echo $following;
                             echo "<td>";
                             echo "<form id='jsform' method='post' action='subscribe.php'>";
                             echo "<input type='hidden' name='id' value=" . $row['Autoid'] . " />";
                             echo "<button class='follow' >Follow</button>";
                             echo "</form>";
                             echo "</td>";                             
                            }
                            else
                            {
                                //echo "<td>false</td>";
                             echo "<td>";
                             echo "<form id='ufform' method='post' action='unsubscribe.php'>";
                             echo "<input type='hidden' name='uid' value=" . $row['Autoid'] . " />";
                             echo "<button class='follow' >UnFollow</button>";
                             echo "</form>";
                             echo "</td>";
                            }
                         }
                        echo "</tr>";
                     }
                } catch (PDOException $e) {
                       'Database Error : ' .$e->getMessage();
                }
            ?>

1 个答案:

答案 0 :(得分:4)

此代码:

    while($follow_row = $sth->fetch(PDO::FETCH_ASSOC))
   {
        $following = $follow_row['Following'];
        $follower = $follow_row['Follower'];
   }

每次获取一行时,只需覆盖$following$follower,然后在变量中获取 LAST 行。也许你想要更像

的东西
    $following[] = $follow_row['Following'];
    $follower[] = $follow_row['Follower'];
             ^^--- append new row value to an array.