2014-02-07 90 views
0

我做的,我希望得到總行數的一個PHP頁面,但它給我的結果爲零,我該怎麼總結NUM在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; 

?> 
+0

你檢查,如果它進入for循環?並且調試'$ res'。或者你有零結果從分貝 –

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

我覺得你只是錯過1行代碼。

+1

我想你有一個額外的行,'$ recepients + = $ record'。將其更新爲'$ recepients' –

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;