简体   繁体   English

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

[英]Select values from table and show it by while?

I want to select values by 'while' but ther is problam, this is the code: 我想通过'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;
}

note $user_id = 1 注意$user_id = 1

The desired output is 期望的输出是

2,3,4 2,3,4

but what I get is 但我得到的是

234 234

The db is like this: 数据库是这样的:

follow table: 按照表格:

id | id | user_follower_id | user_follower_id | user_following_id user_following_id

1 | 1 | 1 | 1 | 2 2

2 | 2 | 1 | 1 | 4 4

3 | 3 | 1 | 1 | 3 3

thanks 谢谢

Do This 做这个

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

since your substr_replace is in loop so every time after creating $article_following_user_id with , it replace the last character every time 因为你的substr_replace是循环的所以每次创建$article_following_user_id,它每次都替换最后一个字符

Edit 编辑

As suggested by Glavić if you can replace your 正如格拉维奇所建议的那样,如果你可以替换你的

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

With

substr($article_following_user_id, 0, -1);

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

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