gpt4 book ai didi

php - while 循环总是打印最后插入的行结果

转载 作者:太空宇宙 更新时间:2023-11-03 10:53:47 26 4
gpt4 key购买 nike

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

最佳答案

这段代码:

    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.

关于php - while 循环总是打印最后插入的行结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22341747/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com