我是 php 新手,我正在关注 Jason Lengstorf 的“面向绝对初学者的 PHP”一书,它会引导您了解如何制作一个基本的博客网站。
它显示给我的代码有很多错误,因为我是 php 新手,所以很难调试代码。
所以我收到了这个警告:警告:array_push() 期望参数 1 是数组,在第 76 行的 C:\xampp\htdocs\simple_blog\inc\functions.inc.php 中给出的布尔值
这是失败的功能
function retrieveEntries($db, $page, $url=NULL)
{
/*
* If an entry URL was supplied, load the associated entry
*/
$fulldisp = NULL;
$e = array();
if(isset($url))
{
$sql = "SELECT id, page, title, entry
FROM entries
WHERE url=?
LIMIT 1";
$stmt = $db->prepare($sql);
$stmt->execute(array($url));
// Save the returned entry array
$e = $stmt->fetch();
// Set the fulldisp flag for a single entry
$fulldisp = 1;
}
/*
* If no entry ID was supplied, load all entry titles for the page
*/
else
{
$sql = "SELECT id, page, title, entry, url
FROM entries
WHERE page=?
ORDER BY created DESC";
$stmt = $db->prepare($sql);
$e = $stmt->execute(array($page));
//$e = NULL; //Declare the variable to avoid errors
// Loop through returned results and store as an array
while($row = $stmt->fetch()) {
if($page=='blog')
{
$e = $row;
$fulldisp = 0;
}
else
{
$e = $row;
$fulldisp = 1;
}
}
//var_dump($row);
/*
* If no entries were returned, display a default
* message and set the fulldisp flag to display a
* single entry
*/
if(!is_array($e))
{
$fulldisp = 1;
$e = array(
'title' => 'No Entries Yet',
'entry' => 'This page does not have an entry yet!'
);
}
}
// Add the $fulldisp flag to the end of the array
//var_dump($e);
array_push($e, $fulldisp); // line 76
return $e;
}
我知道根据消息,变量 $e 是一个布尔值,其值为 false,因为我使用了 var_dump($e) ,然后我将其注释掉。但是在第 76 行之前有一个 if 语句: if(!is_array($e)) 检查 $e 是否不是一个数组,因为 $row 是一个 bool(false) 并且它被分配给 $e。所以我假设因为一个布尔值不是一个数组,所以它会进入这个语句: if(!is_array($e)) 并且在第 76 行的错误之前的正文中将 $e 转回一个数组
我知道这可能真的很令人困惑,但我真的很感激任何帮助,谢谢。