1 回答
TA贡献1890条经验 获得超9个赞
在调用它之前定义空数组。当您运行 foreach 循环时,检查空条件。现在会发生什么,您没有从查询中获得评论,这就是发生这种情况的原因。试试这个。
class Comment {
public static function displayComments($postId) {
$showComments = array(); //this sould be defined in your code
$comments = DB::query('SELECT comments FROM table WHERE post_id=:postid', array(':postid'=>$postId);
if(!empty($comments)){ //check not empty condition.
foreach($comments as $comment) {
$showComments[] = $comment['comment'];
}
}
return $showComments;//this is line 7
}
}
- 1 回答
- 0 关注
- 226 浏览
添加回答
举报
