简体   繁体   English

while循环中仅显示一个结果

[英]Only one result showing in while loop

I'm trying to show all results from the database, but to show "None" if there are no results. 我试图显示数据库中的所有结果,但是如果没有结果,则显示“无”。 However, when I run my code I only get a single result instead of the two that are currently there. 但是,当我运行代码时,只会得到一个结果,而不是当前的两个结果。 I can't figure out what I am doing wrong. 我不知道自己在做什么错。

$select1 = mysqli_query($connect, "SELECT * FROM `update` WHERE did='joined'");
$num_rows = mysqli_num_rows($select1);
if ($num_rows==0) {
    $joined="None";
}else{
    while($row=mysqli_fetch_assoc($select1)) {
        $joined=$row['name'].", ";
    }
}
echo $joined;

You are over writing the data you process in the while loop by using $joined = $row['name'].", "; 您正在使用$joined = $row['name'].", ";来覆盖在while循环中处理的数据$joined = $row['name'].", ";

Instead concatenate all occurances onto the string using the .= operator 而是使用.=运算符将所有出现次数连接到字符串上

while($row=mysqli_fetch_assoc($select1)) {
    $joined .= $row['name'].", ";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM