我无法理解为什么会发生这种错误.从我可以看到我正确使用的函数返回数据库中存在的数据数组.我使用a foreach()来回显数组中的每个数据,但它给出了错误:Warning: Invalid argument supplied for foreach().
这是功能:
// Retrieve posts
function retrieve_posts(){
// start new instance of the database connection
global $dbh;
// Get all the posts
$stmt = $dbh->prepare("SELECT * FROM jacks_barbers_reviews ORDER BY date DESC");
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$result[] = $row;
}
return $result;
}
Run Code Online (Sandbox Code Playgroud)
和(简化的)foreach循环:
<?php
$posts = retrieve_posts();
foreach($posts AS $index){
echo '<div class="test-wrap">'; //contains the individual testimonials
echo '<p>' . $index['post'] . '</p>';
echo '<p style="float:right;font-style:normal;font-size:15px;">By ' .$index['name']. ' ' .$index['date']. '</p>';
echo '<div style="clear:both"></div>';
echo '</div>'; // closes test-wrap
}
?>
Run Code Online (Sandbox Code Playgroud)
那是什么导致了这个错误?
谢谢
| 归档时间: |
|
| 查看次数: |
1003 次 |
| 最近记录: |