简体   繁体   中英

how to get total number of rows fetch from loop?

i make a php page in which i want to get total number of rows,but it gives me result zero,how i sum num of rows in php,here is my code:

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

?>

Try to add into the loop

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

    ?>
<? 
$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.
?>

I think you just miss 1 line of code.

Calculation should be done inside loop,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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