开发者

Invalid argument supplied for foreach(), Cant find why

I am a beginner level php programmer. I was trying to use the MVC but I can't even get past the testing of this one.

Problem: Warning: Invalid argument supplied for foreach() in httpdocs/data/serieDAO.php on line 15

Can anyone give me a tip to what is causing this problem?

The code (sorry if it is very long):

/index.php

<?php 
ini_set('display_errors', 1);
// main controller
require_once ("business/serieservice.php");
$service = new SerieService();
$serielijst = $service->toonAlleSeries();
include("test/test1.php");
?>

/business/serieservice.php

<?php
require_once("data/serieDAO.php");
class SerieService{

    public function toonAlleSeries(){
        $serieDAO = new SerieDAO();
        $lijst = $serieDAO->displayList();
        return $lijst;
    }
}
?>

/data/serieDAO.php

 <?php
require_once("entities/series.class.php");

class SerieDAO{

    // public function __construct(){
        // $dbh = new PDO("mysql:host=localhost;dbname=tvseasons", "seasons", "bompa");
    // }

    public function displayList(){
        $list = array();
        $dbh = new PDO("mysql:host=localhost;dbname=tvseasons", "seasons", "bompa");
        $sql = "select id, title, desc, url, genre, trailer from Series";
        $resultSet = $dbh->query($sql);
        foreach ($resultSe开发者_运维技巧t as $row){
            $serie = new Series($row["id"], $row["title"],$row["desc"], $row["url"], $row["genre"], $row["trailer"]);
            array_push($list, $serie);
        }
        $dbh = null;        
        return $list;
    }


}
?>

/entities/series.class.php

<?
//class met getters en setters van series entity
class Series{
  private $id;
  private $title;
  private $desc; //description
  private $url;
  private $genre;
  private $trailer;

  /* Try to figure out how to determine the $id. Cannot be set obviosly but get should be possible */

  //setters
  function setTitle ($title){
    $this->title = $title;
  }
  function setDesc($desc){
    $this->desc = $desc;
  }
  function setUrl($url){
    $this->url = $url;
  }
  function setGenre($genre){
    $this->genre = $genre;
  }
  function setTrailer($trailer){
    $this->trailer = $trailer;
  }

  //getters
  function getTitle(){
    return $this->title;
  }
  function getDesc(){
    return $this->desc;
  }
    function getUrl(){
    return $this->url;
  }
    function getGenre(){
    return $this->genre;
  }
    function getTrailer(){
    return $this->trailer;
  }
}
?>

/test/test1.php

<html>
<head>
<title>Test1 lijst series</title>
</head>
<body>
<h1>Een lijst van alle series</h1>
<ul>
<?php
foreach($serielijst as $serie){
 print ("<li>bla bla</li>");
}
?>
</ul>
</body>
</html>

edit: I know the code Inside the foreach is not working yet. But I already figured out that that is not the cause.


Your query is failing to return an array with the result-set, because you are using a reserved word (desc) for one of the table columns.

Change:

$sql = "select id, title, desc, url, genre, trailer from Series";

to:

$sql = "select id, title, `desc`, url, genre, trailer from Series";


The error typically means that the argument for the foreach is not an array, or an empty array. You can try to look at the contents of your $resultSet by doing var_dump($resultSet). I'm guessing you're getting an empty reply from MySQL which is resulting in that error. To verify that, you should attempt to run the query manually.


Please vardump $resultSet variable and see if it is an array. Foreach method excepts only arrays and thus, it will throw an error is it is supplied with anything else.


Seems like either the connection or the query fails. Try the following to see what is wrong:

if ($resultSet) {
        foreach ($resultSet as $row){
            $serie = new Series($row["id"], $row["title"],$row["desc"], $row["url"], $row["genre"], $row["trailer"]);
            array_push($list, $serie);
        }
else {
    var_dump($dbh->errorInfo());
}


may be $resultSet is not array. it may happen because sometimes query doesnot get data .

you should add this condition before foreach loop

if(is_array($resultSet) && count($resultSet)>0){

foreach($resultSet as $row){
}

}


 $dbh = new PDO("mysql:host=localhost;dbname=tvseasons", "seasons", "bompa"); 
 $sql = "select id, title, desc, url, genre, trailer from Series";

check these lines, your connection may have error, or check your query which matches your table fields. check manually, is the select query is executing.


Looks like you do not fetch the datas in /data/serieDAO.php, like this :

     $resultSet = $dbh->query($sql);
     $rows = $resultSet->fetchAll(PDO::FETCH_ASSOC); // this looks missing in your code
    foreach($rows as $key => $value) {
        $data_array[$key] = $value;// replace with your "serie" stuffs
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜