Karuppiah RK
Karuppiah RK

Reputation: 3964

while loop always print last inserted row result

Problem is while loop always showing last inserted row result. I've tried this below code to follow/unfollow button option. For example I'm a user id=1. I have already followed user id=4. Now, I want to follow user id=5. When i click follow button(id=5) it turns into Unfollow properly. But, I have already followed user id=4. That turns into Follow. This is my problem.

Then I tried echo $following;. it Prints 5555. That means last inserted data. But I want 45. I'm sure I've made a mistake in my while loop. But I don't know what I should change?

        <?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();
                }
            ?>

Upvotes: 0

Views: 244

Answers (1)

Marc B
Marc B

Reputation: 360592

This code:

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

Simply OVERWRITES $following and $follower every time you fetch a row, leaving you with the LAST row fetched in the variables. Perhaps you want something more like

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

Upvotes: 4

Related Questions