2013-09-22 52 views
0

我想 '而' 來選擇值,但療法是problam,這是代碼:從表中選擇值並按while顯示

<?php 
$following_select_article = mysql_query("SELECT * FROM follow WHERE user_follower_id='$user_id'"); 

while($following_select_article_row = mysql_fetch_array($following_select_article)) { 
    $article_following_user_id = $following_select_article_row['user_following_id'].","; 
    $mmnnss = substr_replace($article_following_user_id, "", -1); 
    $echo $mmnss; 
} 

$user_id = 1

所需的輸出是

2,3,4

但我得到的是

的DB是這樣的:

如下表:

ID | user_follower_id | user_following_id

1 | 1 | 2

2 | 1 | 4

3 | 1 | 3

感謝

回答

2

做這

<?php 

$following_select_article = mysql_query("SELECT * FROM follow WHERE user_follower_id='$user_id'"); 
$article_following_user_id = ""; 
while($following_select_article_row = mysql_fetch_array($following_select_article)){ 

    $article_following_user_id .= $following_select_article_row['user_following_id'].","; 

} 
$mmnnss = substr_replace($article_following_user_id, "", -1); 
echo $mmnnss; 

因爲你substr_replace是這樣每次循環創建$article_following_user_id,後,每次更換的最後一個字符

編輯

由於建議的Glavić,如果你可以取代你

substr_replace($article_following_user_id, "", -1); 

隨着

substr($article_following_user_id, 0, -1); 
+0

非常感謝你第一次的答案和第二的編輯我的問題:) –

+0

不客氣, – zzlalani

+1

無需'substr_replace(...)',只需使用'substr($ str,0,-1);' –

相關問題