如何从循环中获取总行数?

时间:2014-02-07 12:28:22

标签: php

我创建了一个php页面,其中我想获得总行数,但它给我的结果为零,我如何总结php中的行数,这是我的代码:

<? 
$recepients=0;
$count=count($_POST['events']);
for($i=0; $i<$count; $i++){
  $select="select b.first_name AS first_name,b.last_name AS last_name from buyers b,registrations r where b.buyer_id=r.buyer_id and r.event_id='".$_POST['events'][$i]."' group by r.buyer_id";
  $res = $GLOBALS ['mysqli']->query ($select) or die ($GLOBALS ['mysqli']->error . __LINE__);
  $record=mysqli_num_rows($res);
}
echo $recepients+=$record;

?>

3 个答案:

答案 0 :(得分:2)

尝试添加到循环中

    <? 
    $recepients=0;
    $count=count($_POST['events']);
    for($i=0; $i<$count; $i++){
                    $select="select b.first_name AS first_name,b.last_name AS last_name from buyers b,registrations r where b.buyer_id=r.buyer_id and r.event_id='".$_POST['events'][$i]."' group by r.buyer_id";
      $res = $GLOBALS ['mysqli']->query ($select) or die ($GLOBALS ['mysqli']->error . __LINE__);
      $record=mysqli_num_rows($res);

      $recepients = $recepients + $record;
    }
    echo $recepients;

    ?>

答案 1 :(得分:1)

<? 
$recepients=0;
$count=count($_POST['events']);
for($i=0; $i<$count; $i++){
    $select="select b.first_name AS first_name,b.last_name AS last_name from buyers b,registrations r where b.buyer_id=r.buyer_id and r.event_id='".$_POST['events'][$i]."' group by r.buyer_id";
    $res = $GLOBALS ['mysqli']->query ($select) or die ($GLOBALS ['mysqli']->error . __LINE__);
    $record = mysqli_num_rows($res);
    $recepients += $record; // i think you're missing this
}
echo $recepients; // edited - forgot to edit this one.
?>

我想你只是错过了一行代码。

答案 2 :(得分:1)

计算应在循环内完成,

   $recepients = 0; 
   for($i=0; $i<$count; $i++){
            $select="select b.first_name AS first_name,b.last_name AS last_name from buyers b,registrations r where b.buyer_id=r.buyer_id and r.event_id='".$_POST['events'][$i]."' group by r.buyer_id";
            $res = $GLOBALS ['mysqli']->query ($select) or die ($GLOBALS ['mysqli']->error . __LINE__);
            $record=mysqli_num_rows($res);
            $recepients += $record;
    }
    echo $recepients;