Php 如何检查我的foreach循环是否有bug?
我只是快速地为一个文章网站拼凑了一个脚本,在那里我从数据库中检索文章 这是我的index.php脚本Php 如何检查我的foreach循环是否有bug?,php,content-management-system,Php,Content Management System,我只是快速地为一个文章网站拼凑了一个脚本,在那里我从数据库中检索文章 这是我的index.php脚本 <?php // include header require("include/header.php"); require("include/helperfunctions.inc.php"); ?> <!-- page content --> <!-- left section --> &l
<?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");
?>
我得到这个错误:
警告:mysqli_fetch_array()希望参数1是mysqli_result,在第17行的C:\Apache24\htdocs\include\functions.php中给出布尔值最近的文章注意:未定义变量:第14行的C:\Apache24\htdocs\home.php中的文章警告:为第14行的C:\Apache24\htdocs\home.php中的foreach()提供的参数无效
$articles对象似乎存在范围问题。您可以在displayArticles()函数中创建$articles。。但是在函数之外,您的其他代码不知道它。尝试在displayArticles()内返回$articles,并替换displayArticles();在第一页顶部使用以下命令调用:
$articles = displayArticles();
此外,正如panique在评论中指出的,您在foreach块中引用了错误的对象。删除每个$article[blah]末尾的“s”,使其显示为$article[blah]。…有什么问题吗?出现了什么错误?警告:mysqli_fetch_array()希望参数1是mysqli_result,在第17行的C:\Apache24\htdocs\include\functions.php中给出布尔值。最近的文章注意:未定义变量:第14行的C:\Apache24\htdocs\home.php中的文章警告:为foreach()提供的参数无效在C:\Apache24\htdocs\home.php的第14行,很抱歉格式化。希望你能看到3条警告。这绝对是一个很大的帮助,康纳。
$article['id']
而不是$articles['id']
谢谢!我刚加了这一行。。返回$articles[];。现在我遇到了这个错误致命错误:无法使用[]读取第24行的C:\Apache24\htdocs\include\functions.php。请删除括号(return$articles;)。很抱歉打扰您,我感谢您的帮助。。我已经修复了我认为的错误并插入了错误检查。。。现在我得到了这个错误,请注意:第18行的C:\Apache24\htdocs\index.php中的未定义索引:summary。我在谷歌上搜索过,似乎什么也找不到。有什么想法吗?啊,看看这个页面,了解数组中的索引是什么:;在这个上下文中(PHP数组)的索引只是关键字的另一个词:),您对我来说一点也不麻烦,我很高兴能够提供帮助。其他人可能会告诉你重新学习编程的基础知识(数组、函数、范围等)。。这将需要更多的时间,但是阅读PHP.net和维基百科中关于这些概念的内容将回答这些和未来的问题,这符合stackoverflow的精神:做大量自己的研究。。然后带着你已经尝试过的东西来到这里:)
$articles = displayArticles();