[英]how to count the number of rows of a query inside a while loop?
i have the following php code 我有以下PHP代码
$thread_pick=mysqli_query($db,'SELECT * FROM u_mess WHERE participant_1="'.$display_name.'" OR participant_2="'.$display_name.'" ') or trigger_error(mysqli_error());
if(mysqli_num_rows($thread_pick) >= 1 )
{
while($row = mysqli_fetch_assoc($thread_pick))
{
$thread_pick_sub = mysqli_query($db,'SELECT * FROM u_mess_thread WHERE (thread_id="'.$row['thread_id'].'" AND mess_to="'.$display_name.'" )')or trigger_error(mysqli_error());
echo mysqli_num_rows($thread_pick_sub).'dsddsds';
$mess_num_count = mysqli_num_rows($thread_pick_sub);
}
}
my problem:the table has two users user 1 has sent 3 messages and user 2 has sent 2 messages now the expected value inside variable $mess_num_count
should be 5 but it is showing 2 what do i do to make it work 我的问题:该表有两个用户,用户1发送了3条消息,用户2发送了2条消息,现在变量$mess_num_count
内的$mess_num_count
应为5,但它显示2我该怎么做才能使其正常工作
I think it's only counting the messages from user 2
so it's 2
! 我认为这只是计算来自user 2
的消息,所以它是2
!
To get 5
you have to use the operator +=
like this: 要获得5
您必须使用运算符+=
如下所示:
$mess_num_count += mysqli_num_rows($thread_pick_sub);
The code should then look like this: 然后,代码应如下所示:
$thread_pick=mysqli_query($db,'SELECT * FROM u_mess WHERE participant_1="'.$display_name.'" OR participant_2="'.$display_name.'" ') or trigger_error(mysqli_error());
$mess_num_count = 0;
if(mysqli_num_rows($thread_pick) >= 1 )
{
while($row = mysqli_fetch_assoc($thread_pick))
{
$thread_pick_sub = mysqli_query($db,'SELECT * FROM u_mess_thread WHERE (thread_id="'.$row['thread_id'].'" AND mess_to="'.$display_name.'" )')or trigger_error(mysqli_error());
echo mysqli_num_rows($thread_pick_sub).'dsddsds';
$mess_num_count += mysqli_num_rows($thread_pick_sub);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.