Warning: Invalid argument supplied for foreach()

Asked

Viewed 9,626 times

2

I have the following select:

public function SelectLastError(){
        try {
            $stmt = $this->conn->prepare("SELECT Name, Data FROM Errors ORDER BY ErrorsId DESC LIMIT 3");
            $stmt->execute();
            while ($row = $stmt->fetch( PDO::FETCH_ASSOC )){
                $rows[] = $row;
            }
        }catch (PDOException $exception){
            echo  $exception->getMessage();
            echo "Error!";
            return null;
        }
    }

And I’m trying to call it in an HTML like this:

<?php 
  $rows = $selectors->SelectLastError();
  foreach ($rows as $row):?>                            
     <a href="" class="list-group-item">
      <i class="fa fa-bug"></i><?php echo $row['Name'];?>
      <span class="pull-right text-muted small"><em><?php echo $row['Data']?></em>
      </span>
     </a>                                
  <?php endforeach; ?>

Only he gives the title mistake : "Warning: Invalid argument supplied for foreach() in"

I’ve made the same idea in another foreach, I do not know why is appearing this error, SELECT this right already tested. Does anyone have any idea?

  • Ironically this means that there is no error o.o. I believe the way is to check if there is any value in $rows

  • @rray Worst that has an error in hehehe function, I found missing a return $rows; in the end

1 answer

5


Error because your method does not return an iterable value via foreach.

The method SelectLastError should return a array, since you want to use it later in a foreach.

You are adding the results in $rows. Then you should also have returned this value.

Behold:

function SelectLastError()
{
    try {
        $stmt = $this->conn->prepare("SELECT Name, Data FROM Errors ORDER BY ErrorsId DESC LIMIT 3");
        $stmt->execute();
        while ($row = $stmt->fetch( PDO::FETCH_ASSOC )){
            $rows[] = $row;
        }

        return $rows;

    }catch (PDOException $exception){
        echo  $exception->getMessage();
        echo "Error!";
        return null;
    }
}

Recommending

If you want to group the results, row by row, into array and, in the end, return it to use in a function, it would be easier to use a solution that is already present in the language. Instead of using a while and save result by result on array, recommend modifying the method. You will replace the whole of the while for fetchAll.

Behold:

function SelectLastError () {

    try {

        $stmt = $this->conn->prepare("SELECT Name, Data FROM Errors ORDER BY ErrorsId DESC LIMIT 3");

        $stmt->execute();

        return $stmt->fetchAll( PDO::FETCH_ASSOC);

    }catch (PDOException $exception){
        echo  $exception->getMessage();
        echo "Error!";
        return null;
    }
}
  • That’s right I miss the Return, I just saw it. Thank you

  • 1

    Even so, I recommend you do as I taught you in the second option. If you do it the way it is today, you will go for a walk, since PHP already has it ready.

  • Done! Thank you so much for the tip!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.