PHP从数据库中选择并返回值

用户11488433

我想在多页上显示评论,所以我想从数据库的1页上选择评论,并在所有其他页面上包括这些评论。

我可以选择并显示每个评论,但是某些帖子没有评论。现在,我在没有评论的情况下收到每个帖子的错误。Notice: Undefined variable: showComments in ...\comments.php on line 7


选择评论的页面:

class Comment {
          public static function displayComments($postId) {
                  $comments = DB::query('SELECT comments FROM table WHERE post_id=:postid', array(':postid'=>$postId);
                  foreach($comments as $comment) {
                          $showComments[] = $comment['comment'];
                  }
                          return $showComments;//this is line 7
          }
}

其他页面:

$postOutput = "postImg, postLikes, postLikeButton";

if(Comment::displayComments($post['id']) >= 1) {
           $comments = Comment::displayComments($post['id']);
           foreach ($comments as $comment) {
                   $postOutput .= $comment;
           }
}

$postOutput .= "postCommentForm";
echo $postOutput;
迪利普·希拉帕拉

在调用它之前定义空数组。并且当您运行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
          }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从sqlite数据库中选择不同的值对

来自分类Dev

如何设置radcombobox从数据库中选择的值?

来自分类Dev

从Wordpress数据库的单个列中选择多个值

来自分类Dev

根据输入的值从数据库中选择ViewName

来自分类Dev

如何处理数据库的零返回值?

来自分类Dev

让用户从数据库PHP中选择某人

来自分类Dev

获取从数据库中选择的checkbx值

来自分类Dev

php oop从数据库中选择数据

来自分类Dev

VueJs从数据库中选择默认值

来自分类Dev

我可以使Python数据库选择查询仅返回值吗

来自分类Dev

从数据库中选择值为X的表

来自分类Dev

尝试从Joomla 2.5数据库中选择单个值

来自分类Dev

C#检查是否从数据库返回值

来自分类Dev

从sqlite数据库中选择不同的值对

来自分类Dev

Linq查询未从数据库字段返回值

来自分类Dev

PHP:从数据库中选择

来自分类Dev

我想从数据库中选择值

来自分类Dev

如何设置radcombobox从数据库中选择的值?

来自分类Dev

在“数据库”列中选择值数

来自分类Dev

当前未从MYSQL数据库分配的返回值

来自分类Dev

从数据库中选择没有值组合的项目

来自分类Dev

Java数据库未返回值

来自分类Dev

使用PHP查询从SQL数据库为Sagepay返回值

来自分类Dev

从数据库中选择单个值

来自分类Dev

附加<option>并从数据库中选择值

来自分类Dev

从数据库中选择唯一值

来自分类Dev

无法从数据库中选择 PHP 加载的元素值

来自分类Dev

LINQ 从数据库中选择值

来自分类Dev

编辑显示从数据库中选择的下拉值的 Servlet