我正在迅速将一个脚本放在一个文章网站上,我从数据库中检索文章。
这是我的index.php脚本
<?php
// include header
require("include/header.php");
require("include/helperfunctions.inc.php");
?>
<!-- page content -->
<!-- left section -->
<section id="contentleft">
<?php require("include/functions.php");
displayArticles();
foreach ($articles as $article) : ;
?>
<h2>Recent Articles</h2>
<ul>
<li><?php echo htmlout($articles['id']) ; ?></li>
<li><?php echo htmlout($articles['title']) ; ?></li>
<li><?php echo htmlout($articles['summary']) ; ?></li>
</ul>
<?php endforeach; ?>
</section>
<!-- right content -->
<section id="contentright">
</section>
<?php
// include footer
require("include/footer.php");
?>
这是函数库的开头
function displayArticles($order="publicationdate DESC"){
// connect to the database
include("include/db.inc.php");
$query = "SELECT id, title, summary FROM maths order by ". $order . " limit 10";
// query the database
$result = mysqli_query($link, $query);
// error checking
if(mysqli_connect_errno()){
// $error = "error fetching articles";
echo " could not connect: " . mysqli_connect_errno() . " ";
exit();
}
// loop through storing values into array
while($row = mysqli_fetch_array($result)){
$articles[] = array('id'=>$row['id'] , 'title'=>$row['title'],'summary'=>$row['summary']);
}
}
?>
我收到此错误:
警告:mysqli_fetch_array()期望参数1为mysqli_result,第17行的C:\ Apache24 \ htdocs \ include \ functions.php中给出布尔值。最近的文章提示:未定义的变量:C:\ Apache24 \ htdocs \ home中的文章第14行的.php警告:在第14行的C:\ Apache24 \ htdocs \ home.php中为foreach()提供的参数无效
答案 0 :(得分:2)
它看起来像$ articles对象的范围问题。您在displayArticles()函数中创建$文章..但在函数之外,您的其他代码不知道它。尝试在displayArticles()中返回$文章,并替换你的displayArticles();使用以下命令在第一页的顶部调用:
$articles = displayArticles();
另外,正如评论中的panique所指出的那样,你在foreach块中引用了错误的对象。删除每个$ articles [blah]末尾的's',以便它读取$ article [blah]。