从数据库中得到错误的结果-使用PHP对记录中的特定字段进行错误计数


Getting wrong result from database - Wrong counting for a specific field in records using PHP

我正在尝试使用PHP计算存储在数据库记录中的单词"Saturday"的数量。它没有正确计数。我在那栏里只有三条记录:星期六、星期六、周日当我打印$row["Repeating"]时,它会将所有记录显示为Saturday Saturday,其中Saturday的计数器必须为2,而Sunday的计数器为1。我的计数器工作不正常星期六我拿到的柜台价是3英镑,星期天是3英镑。我看不出这个错误。有什么想法我会很感激的。

谢谢

        $numberofWcounters = mysqli_num_rows($result);
        $dementiacounter1 = 1;
        // count Saturday.
         $counterSaturday=0;
        // count sunday
          $counterSunday=0;
        while( $counter <= $numberofWcounters)
         {

            if ($row['Day'] ='Saturday')
             {
                $counterSaturday++;
             }
           if ($row['Repeating'] ='Sunday')
             {
                $counterSunday++;
             }
             $counter++;
        }
          echo $row['Repeating'] ;
          echo "<br />The number of Saturday for repeating question are :   " . $$counterSaturday ;

您似乎没有在结果集上迭代,这意味着您在同一行(结果集中的第一行)上循环3次

您必须使用mysqli_fetch或等效工具来确保实际访问查询生成的每个不同结果。

$result->bind_result( $day, $repeating );
while( $result->fetch() ) {
  // in every iteration of this loop, $day is $row['Day'] and $repeating is $row['Repeating'] of a new row
  if ($day =='Saturday')
  {
    $counterSaturday++;
   }
   if ($repeating == 'Sunday')
   {
    $counterSunday++;
   }
}