如果我的数据库为空然后显示警告

if my database is empty then showing warning

如果我的数据库为空,则在我的本地主机上显示警告和通知消息

         <?php
           while ($row = mysqli_fetch_array($tasks)){ 
              if (isset($row)){
                  $rows[] = $row;
              }
           }
          ?>                 
           Count is: <?php echo count($rows); ?>             
          <?php
              foreach ($rows as $row_id  => $row){
          ?>  

请帮我解决这个问题。

您应该在加载之前声明变量 $rows,以防没有行。

   $rows = [];
   while ($row = mysqli_fetch_array($tasks))
   { 
      $rows[] = $row;
   }

如果您不这样做,$rows 将只设置在您的 while 循环中,如您所见。

您可以将其缩短为

$rows = mysqli_fetch_all ($tasks, MYSQLI_BOTH);

只是对您当前代码的改进,因为上面的 post 已经回答了它。

<?php
    $rows = [];
    while ($row = mysqli_fetch_array($tasks)){ 
       if (isset($row)){
           $rows[] = $row;
       }
    }
?>                 
    Count is: <?= count($rows) ?> //You'll notice that I changed this line 
    //to a shorter version. since "<?= ?>" will automatically "echo" anything within 
    //it so you don't have to write echo and a closing ";"

      <?php
          foreach ($rows as $row_id  => $row){
          //let's clean your checkbox code here to make it more 
          //understandable
          $checked = ($row['status'])?"checked":"";//This is a ternary operator you can read [here][1]
          echo '<input type="checkbox" id="task" '.$checked.'>';
      ?>               
          <td class= "task"><?= $row['task'] ?></td>
    <?php } ?>

快乐编码