Php 尝试获取非对象的属性&;为foreach()提供的参数无效

Php 尝试获取非对象的属性&;为foreach()提供的参数无效,php,json,Php,Json,fruitget.php <?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "ssl"; $dbh = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); $sqlquery = $dbh-&

fruitget.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ssl";
$dbh = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);

$sqlquery = $dbh->prepare("select id, fruitname, fruitcolor, fruitimage from fruits order by rand() limit 1");
$sqlquery->execute();
$result = $sqlquery->fetchAll();
$result = array("fruits"=>$result);
header("Content-type:application/json");
$jsonfile = json_encode($result);
echo $jsonfile;

?>

感谢您的帮助。谢谢。这是为一个班级准备的,它完全按照老师的描述复制,foreach没有错误,但我有错误。

file\u get\u内容需要是一个URL,而不是指向直接文件。这就是为什么它是空的。问题已回答。

检查$content中的内容是否如此困难<代码>变量转储($content)它是空的。为什么它是空的?试着学一点。。。。现在显示$jcontent中的内容,也许您最终会自己看到这盏灯#
<?php

$jcontent = file_get_contents("fruitget.php");
$content = json_decode($jcontent);

?>

<div class="ad">
 <h1>Fruit of the Day</h1>
 <?php
 foreach ($content->fruits as $fruit) {
    echo '<span>' . $fruit->fruitcolor . ' ' .$fruit->fruitname . '</span>';
    echo "<img src='".$fruit->fruitimage."'width='300'/>";
 }
 ?>
foreach ($content->fruits as $fruit) {