Php 为foreach()提供的参数无效,找不到原因
我是一名初级php程序员。我试图使用MVC,但我甚至无法通过这一测试 问题:警告:第15行的httpdocs/data/serieDAO.php中为foreach()提供的参数无效 谁能告诉我是什么导致了这个问题 代码(如果很长,请抱歉): /index.phpPhp 为foreach()提供的参数无效,找不到原因,php,foreach,Php,Foreach,我是一名初级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
<?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 ($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;
}
}
?>
/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>
test1lijst系列
Een lijst van alle系列
编辑:我知道foreach中的代码还没有工作。但是我已经发现这不是原因。错误通常意味着foreach的参数不是数组或空数组。您可以通过执行
var\u dump($resultSet)
来查看$resultSet的内容。我猜你从MySQL得到了一个空的回复,这导致了这个错误。要验证这一点,您应该尝试手动运行查询。该错误通常意味着foreach的参数不是数组或空数组。您可以通过执行var\u dump($resultSet)
来查看$resultSet的内容。我猜你从MySQL得到了一个空的回复,这导致了这个错误。要验证这一点,您应该尝试手动运行查询。请使用vardump$resultSet变量并查看它是否为数组。Foreach方法只例外于数组,因此,如果它与任何其他方法一起提供,它将抛出一个错误。请使用vardump$resultSet变量并查看它是否是数组。Foreach方法只例外于数组,因此,如果它与其他任何方法一起提供,它将抛出一个错误。看起来要么连接失败,要么查询失败。请尝试以下操作以查看问题所在:
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());
}
看起来要么连接失败,要么查询失败。请尝试以下操作以查看问题所在:
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());
}
查询无法返回带有结果集的数组,因为您正在对其中一个表列使用保留字(
desc
)
更改:
$sql = "select id, title, desc, url, genre, trailer from Series";
致:
查询无法返回带有结果集的数组,因为您正在对其中一个表列使用保留字(
desc
)
更改:
$sql = "select id, title, desc, url, genre, trailer from Series";
致:
可能是$resultSet不是数组。 这可能是因为有时查询无法获取数据 您应该在foreach循环之前添加此条件 if(is_数组($resultSet)&&count($resultSet)>0){
}可能是$resultSet不是数组。 这可能是因为有时查询无法获取数据 您应该在foreach循环之前添加此条件 if(is_数组($resultSet)&&count($resultSet)>0){
}看起来您没有在/data/serieDAO.php中获取数据,如下所示:
$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
}
看起来您没有在/data/serieDAO.php中获取数据,如下所示:
$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
}
检查这些行,您的连接可能有错误,或者检查与表字段匹配的查询。如果正在执行select查询,请手动检查
检查这些行,您的连接可能有错误,或者检查与表字段匹配的查询。手动检查,是否正在执行select查询。您是否检查过数据库查询是否有结果?Foreach需要一个数组,但没有得到它。检查创建阵列的位置以及哪些阵列可能会失败。在创建PDO对象时使用try/catch,以便查看引发的任何异常。另外,请尝试只发布代码的相关部分。有关错误检查的信息,请转到。就像他们说的,确保你的查询确实返回了一些东西。当我还在使用php时,我这样做是为了在($row=mysql_fetch_assoc($result)){echo$row['firstname'];echo$row['lastname'];echo$row['address'];echo$row['age'];}Jarco考虑将代码减少到下一个问题可以想到的最小非工作示例。事实上,强迫人们阅读100行与问题无关的代码不仅是不礼貌的,而且将代码减少到有问题的部分有助于理解问题并让其他人重现问题。您是否检查过DB查询是否有任何结果?Foreach需要一个数组,但没有得到它。检查创建阵列的位置以及哪些阵列可能会失败。在创建PDO对象时使用try/catch,以便查看引发的任何异常。另外,请尝试只发布代码的相关部分。有关错误检查的信息,请转到。就像他们说的,确保你的查询确实返回了一些东西。当我还在使用php时,我这样做是为了在($row=mysql_fetch_assoc($result)){echo$row['firstname'];echo$row['lastname'];echo$row['address'];echo$row['age'];}Jarco考虑将代码减少到下一个问题可以想到的最小非工作示例。实际上,强迫人们阅读100行与问题无关的代码不仅是不礼貌的,而且将代码缩减到有问题的部分有助于你理解问题,让其他人重现问题。我是手动完成的。你是正确的,错误就在那里。desc在sql中是保留的。我应该知道…我是手工做的。你是正确的,错误就在那里。desc在sql中是保留的。我应该知道…我只是通过在phpmyadmin中运行查询发现了这一点。修好它的好建议。thxI刚刚通过在phpmyadmin中运行查询发现了这一点。修好它的好建议。谢谢