从表中选择值并按时显示?

时间:2013-09-22 05:29:51

标签: php

我想通过'while'来选择值,但是他们是problam,这是代码:

<?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-

但我得到的是

  

234

db就像这样:

  

按照表格:

     

id | user_follower_id | user_following_id

     

1 | 1 | 2

     

2 | 1 | 4

     

3 | 1 | 3

感谢

1 个答案:

答案 0 :(得分: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);