如何计算while循环中查询的行数?

时间:2014-11-04 22:49:20

标签: php database count while-loop

我有以下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);
  }
}

我的问题:该表有两个用户,用户1发送了3条消息,而用户2已发送了2条消息,现在变量$mess_num_count内的预期值应为5,但它显示2我该怎么做才能使它工作

1 个答案:

答案 0 :(得分:0)

我认为它只计算来自user 2的邮件,因此它2

要获得5,您必须使用此类运算符+=

$mess_num_count += mysqli_num_rows($thread_pick_sub);

代码应如下所示:

$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);
  }
}