Muhammad Arif
Muhammad Arif

Reputation: 1022

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;

?>

Upvotes: 0

Views: 92

Answers (3)

Dev
Dev

Reputation: 479

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;

    ?>

Upvotes: 2

Rikesh
Rikesh

Reputation: 26431

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;

Upvotes: 1

Craftein
Craftein

Reputation: 762

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

Upvotes: 1

Related Questions