Php 为什么我会得到这个警告:array_push()希望参数1是array,布尔值在中给出?
我是php新手,我正在学习Jason Lengstorf的《php for absolute初学者》一书,它将指导您如何创建一个基本的博客网站 它向我展示的代码有很多错误,因为我是php新手,所以调试代码很困难 所以我得到了这个警告:警告:array\u push()期望参数1是数组,布尔值在第76行的C:\xampp\htdocs\simple\u blog\inc\functions.inc.php中给出 这是它失败的函数Php 为什么我会得到这个警告:array_push()希望参数1是array,布尔值在中给出?,php,Php,我是php新手,我正在学习Jason Lengstorf的《php for absolute初学者》一书,它将指导您如何创建一个基本的博客网站 它向我展示的代码有很多错误,因为我是php新手,所以调试代码很困难 所以我得到了这个警告:警告:array\u push()期望参数1是数组,布尔值在第76行的C:\xampp\htdocs\simple\u blog\inc\functions.inc.php中给出 这是它失败的函数 function retrieveEntries($db, $pag
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;
}
我知道根据消息varriable$e是一个布尔值,它的值为false,因为我使用了var_dump($e),然后将其注释掉。但是在第76行之前有一个if语句:if(!is_array($e)),它检查$e是否不是一个数组,因为$row是bool(false),并且分配给$e。因此,我假设,因为布尔值不是数组,所以它将输入以下语句:if(!is_array($e)),在第76行的错误之前的语句体中,将$e变回数组
我知道这可能会让人困惑,但我非常感谢您的帮助。$e=$stmt->execute(array($page))代码>这就是问题所在吗?为什么在这里使用$e
您需要如果($stmt->execute(array($page)){…}
$e不能保证是代码中的数组,那么有几个代码路径会产生非数组值$e
我猜是在这条线上
$e = $stmt->fetch();
或
是设置非数组值的位置
在将$e用作数组之前,先测试它是否是数组,或者在所有代码路径都导致$e成为数组的情况下构造代码 为什么要使用array_push而不是$e[]=$fulldisp
?我想在代码中添加一些调试,以查看if语句的哪个分支正在运行。您将$e作为布尔值,因为我认为其中一个SQL查询失败,但代码中没有错误检查,因此您看不到错误是什么。如果数据库查询没有返回结果,$e的值是多少?请详细说明代码中发生错误的位置。我们没有对行进行编号,我也看不到您正在调用的代码中的任何位置array\u push
或检查is\u array
@sh1ftst0rm他在最末端调用array push以在条目数组的末尾添加显示标志,以便使用此函数传递这两个行
$e = $stmt->execute(array($page));