我是初级级别的php程序员.我试图使用MVC,但我甚至无法通过这个测试.
问题:警告:在第15行的httpdocs/data/serieDAO.php中为foreach()提供的参数无效
任何人都可以给我一个提示导致这个问题的原因吗?
代码(对不起,如果它很长):
的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");
?>
Run Code Online (Sandbox Code Playgroud)
/business/serieservice.php
<?php
require_once("data/serieDAO.php");
class SerieService{
public function toonAlleSeries(){
$serieDAO = new SerieDAO();
$lijst = $serieDAO->displayList();
return $lijst;
}
}
?>
Run Code Online (Sandbox Code Playgroud)
/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 ($resultSet 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;
}
}
?>
Run Code Online (Sandbox Code Playgroud)
/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;
}
}
?>
Run Code Online (Sandbox Code Playgroud)
/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>
Run Code Online (Sandbox Code Playgroud)
编辑:我知道foreach里面的代码还没有工作.但我已经发现这不是原因.
您的查询无法返回带有结果集的数组,因为您正在desc
为其中一个表列使用保留字().
更改:
$sql = "select id, title, desc, url, genre, trailer from Series";
Run Code Online (Sandbox Code Playgroud)
至:
$sql = "select id, title, `desc`, url, genre, trailer from Series";
Run Code Online (Sandbox Code Playgroud)