Php 如果我的数据库为空,则显示警告

Php 如果我的数据库为空,则显示警告,php,warnings,notice,Php,Warnings,Notice,如果我的数据库为空,则在本地主机上显示警告和通知消息 <?php while ($row = mysqli_fetch_array($tasks)){ if (isset($row)){ $rows[] = $row; } } ?> Count is: &

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

         <?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
将只在
中设置,而正如您所看到的那样,
循环

你可以把这个缩短到

$rows = mysqli_fetch_all ($tasks, MYSQLI_BOTH);

如果没有行,则应在加载之前声明变量
$rows

   $rows = [];
   while ($row = mysqli_fetch_array($tasks))
   { 
      $rows[] = $row;
   }
如果不这样做,
$rows
将只在
中设置,而正如您所看到的那样,
循环

你可以把这个缩短到

$rows = mysqli_fetch_all ($tasks, MYSQLI_BOTH);

这只是对您当前代码的一个改进,因为上面的帖子已经给出了答案

<?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 } ?>

计数是://您会注意到我更改了这一行
//到一个较短的版本。因为“”将自动“回显”其中的任何内容
//这样你就不必写回音和结束语了”

这只是对您当前代码的一个改进,因为上面的帖子已经给出了答案

<?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 } ?>

计数是://您会注意到我更改了这一行
//到一个较短的版本。因为“”将自动“回显”其中的任何内容
//这样你就不必写回音和结束语了”

我加了一个缺少的花括号。但我不确定它应该在哪里。请先尝试向变量$rows添加一个初始值。我添加了一个缺少的大括号。但我不确定它应该在哪里。请先尝试向变量$rows添加一个初始值。