2

I can not understand why this error is occurring. From what I can see I the function I use correctly returns an array of data that exists on the database. I use a foreach() to echo out each of the data in the array but it gives me the error: Warning: Invalid argument supplied for foreach().

Here is the function:

// Retrieve posts
    function retrieve_posts(){
        // start new instance of the database connection 
        global $dbh;

        // Get all the posts
        $stmt = $dbh->prepare("SELECT * FROM jacks_barbers_reviews ORDER BY date DESC");
        $stmt->execute();

        while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

                $result[] = $row;
        }

        return $result;
    }

And the (simplified) foreach loop:

<?php

        $posts = retrieve_posts();


            foreach($posts AS $index){

                echo '<div class="test-wrap">'; //contains the individual testimonials
                echo '<p>' . $index['post'] . '</p>';
                echo '<p style="float:right;font-style:normal;font-size:15px;">By ' .$index['name']. '  ' .$index['date']. '</p>';
                echo '<div style="clear:both"></div>';
                echo '</div>'; // closes test-wrap
            }


    ?>

So what is causing this error?

Thanks

2
  • 3
    If no posts are retrieved by your SQL query, then you'll be a null return from the function because $result is undefined.... define it as an empty array before your while loop in retrieve_posts() Commented Jun 18, 2013 at 20:37
  • How about doing return $stmt->fetchAll() to get all the rows? I cannot se anything wrong with the foreach as long as post and name really are columns of jacks_barbers_reviews. Consider not using *. Commented Jun 18, 2013 at 20:55

1 Answer 1

6

I guess this might happen if you have no results. You didn't initialize the $result variable in the retrieve_posts function. Null is returned and this is not an allowed value to be iterated inside foreach loop.

Try initializing the variable: $result = array(); before the while loop.

edit: Take a look at foreach official PHP docs:

The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects

If you haven't initialized it, you return a null value which is neither an object nor an array.

Sign up to request clarification or add additional context in comments.

4 Comments

That'll teach me for not bothering to initialise my arrays before using them. Thanks.
@crm: this is a major weakness of PHP comparing to python. If you wrote something strange (or even completely wrong), PHP interpreter will often try to figure out what you meant. And this leads to such situations.
@Pinoniq That doesn't seem to retrieve the results. Where as fetch(PDO::FETCH_ASSOC) does, I think it's a weird PHP thing?
BTW, try to avoid using global. This is a very ugly approach and many people will likely blame you for using it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.