[英]Difficulty in printing total number of rows in php MySQL?
I'm trying to fetch 5 random followers of the current users. 我正在尝试获取当前用户的5个随机关注者。 And then I'll fetch all the posted by the 5 random followers but I'm having trouble doing this.
然后,我将获取5个随机关注者的所有帖子,但这样做有麻烦。 I want to print the total number of rows, but I'm getting individual rows posted by each follower.
我想打印总行数,但是我得到了每个关注者发布的单独行。
<?php
mysql_connect("localhost","robin","mypassword");
mysql_select_db("my_db");
//Get list of posts to be paginated
$sql = mysql_query(
"SELECT * FROM followers
WHERE being_followed = '".$_SESSION['username']."' LIMIT 5"
);
while($followers = mysql_fetch_array($sql)){
$my_followers = $followers['follower_username'];
$new_query = mysql_query("SELECT * FROM posts WHERE poster = '$my_follower'");
while($the_posts = mysql_fetch_array($new_query)){
$total_posts = mysql_num_rows($new_query);
$echo $total_posts;
$poster = $the_posts['poster'];
$the_post = $the_posts['the_post'];
echo $poster;
echo "<br>";
echo $the_post;
echo "<p>";
}
}
?>
The above code print: 上面的代码打印:
3 Robin 3罗宾
hi guys this is robin 嗨,大家好,我是罗宾
3 Robin 3罗宾
I'm loving it 我喜欢这个
3 Robin 3罗宾
Where's he? 他在哪
2 Sam going through changes 2山姆经历变革
2 Sam where's da party 2nite 2山姆在哪里聚会2nite
But that's not what I want. 但这不是我想要的。 I simply want to print the total number of posts.
我只想打印职位总数。
Please help me out guys. 请帮我。
Thanks in advance. 提前致谢。
Try this: 尝试这个:
$totalPosts = 0;
while($followers = mysql_fetch_array($sql)){
$my_follower = $followers['follower_username'];
$new_query = mysql_query("SELECT * FROM posts WHERE poster = '$my_follower'");
$num_rows = mysql_num_rows($new_query);
$totalPosts += $num_rows;
echo $num_rows." ".$my_follower."<br/>";
}
echo $totalPosts ;
It will output something like: 它将输出类似:
3 Robin
3罗宾
2 Sam2山姆
Updated to include total number of posts 已更新,包括帖子总数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.