Search code examples
phpwarningsnotice

if my database is empty then showing warning


If my database is empty then showing the warning and notice messages on my localhost

         <?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){
          ?>  

please help me to clear this problem.


Solution

  • You should declare the variable $rows before your load in case there are no rows.

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

    If you don't do this, $rows will only be set in your while loop as you've seen.

    You could shorten this to

    $rows = mysqli_fetch_all ($tasks, MYSQLI_BOTH);