Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Prepared Statement Foreach Loop

The code below only returns one result. The remaining five results are blank. How can I return all rows?

foreach($dates as $date){

    if($stmt->prepare("SELECT event FROM calendar WHERE date = ?")) {

        $stmt->bind_param('i',$date);

        $stmt->execute();

        $stmt->bind_result($event);

        $stmt->store_result();

        while($stmt->fetch()) {
            echo $event;
        }
        $stmt->close();
    }
}
like image 657
Mr. Jones Avatar asked Nov 23 '11 16:11

Mr. Jones


1 Answers

You closed the prepared statement in the first execution of foreach loop.

Move your $stmt->prepare("SELECT event FROM calendar WHERE date = ?") outside of the foreach loop and

$stmt->close() outside of foreach loop

like image 114
robert Avatar answered Oct 13 '22 00:10

robert