2016-11-17 184 views
0

我試圖顯示數據庫的所有結果,但如果沒有結果顯示「無」。但是,當我運行我的代碼時,我只得到一個結果而不是當前存在的兩個結果。我無法弄清楚我做錯了什麼。在while循環中只顯示一個結果

$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; 

回答

1

您可以利用$joined = $row['name'].", ";

在寫你在while循環處理數據,而不是使用.=操作

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

謝謝您連接所有出現在字符串!但是,現在這不止一次地添加每個結果。所以我看到user1,user2,user1,user2 – Rheanna

+0

使用fetch_array而不是assoc。 – 2016-11-17 18:13:36

+0

我仍然得到每個數據庫條目的多個結果... – Rheanna