【问题标题】:How to create JSON nested object using php pdo如何使用 php pdo 创建 JSON 嵌套对象
【发布时间】:2017-08-23 11:08:36
【问题描述】:

我在 stackoverflow 上查看了大多数相关答案,但找不到适合我的情况。

我们将不胜感激。

我正在使用 PHP PDO 从两个名为 cmets 和 cmets_reply 的 mysql 表中获取数据,其值如下

评论表

  • comment_id
  • news_id
  • comment_names
  • comment_date

comment_reply 表

  • reply_id
  • comment_id
  • reply_names
  • 回复
  • 回复日期

我得到空数组作为结果,而不是像这样的 JOSN 格式的结果:

[
   {"id": 1,
    "comment_date": "2017-08-09",
    "comment_time": "06:10:00",
    "names": "Imenwo Alex",
    "img": "c1.jpg",
    "comments": "<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,There are many variations of passages of Lorem Ipsum available, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,There are many variations of passages of Lorem Ipsum available, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>",
    "replies":[
        {
                 "id": 1,
                "reply_date": "2017-08-09",
                "reply_time": "06:10:00",
                "names": "frank Alex",
                "img": "c1.jpg",
                "reply": "<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,There are many variations of passages of Lorem Ipsum available, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,There are many variations of passages of Lorem Ipsum available, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>"
            },
            {
                 "id": 2,
                "reply_date": "2017-08-09",
                "reply_time": "06:10:00",
                "names": "frank Alex",
                "img": "c1.jpg",
                "reply": "<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,There are many variations of passages of Lorem Ipsum available, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,There are many variations of passages of Lorem Ipsum available, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>"
            }
    ]
    },
    {"id": 2,
    "comment_date": "2017-08-09",
    "comment_time": "06:10:00",
    "names": "Imenwo Alex",
    "img": "c1.jpg",
    "comments": "<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,There are many variations of passages of Lorem Ipsum available, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,There are many variations of passages of Lorem Ipsum available, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>",
    "replies":[
            {
                 "id": 1,
                "reply_date": "2017-08-09",
                "reply_time": "06:10:00",
                "names": "frank Alex",
                "img": "c1.jpg",
                "reply": "<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,There are many variations of passages of Lorem Ipsum available, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit,There are many variations of passages of Lorem Ipsum available, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>"
            }
        ]
    }
]

下面是我的 PHP 脚本:

 //
      $q = "SELECT * FROM comments WHERE comments.news_id =:id";         
      //prepare
      $stmt = $this->DB_con->prepare($q);
      //bind parameters
      $stmt->bindParam(':id', $id);

      //create an array
      $json_response = array(); 
      while($obj=$stmt->fetch(PDO::FETCH_OBJ)) {

          $currentComments = array(
              'id' => $obj->comment_id,
              'comment_names' => $obj->comment_names,
              'comment_date' => $obj->comment_date,
              'comment_time' => $obj->comment_time,
              'comment_img' => $obj->comment_img,
              'comments' => $obj->comments,
              'replies' => array()
          );

          //
          $r = "SELECT * FROM comments_reply WHERE comments_reply.comment_id =:id"; 
          //prepare
          $stmt = $this->DB_con->prepare($r);
          //bind parameters
          $stmt->bindParam(':id', $obj->comment_id);
          while($obj=$stmt->fetch(PDO::FETCH_OBJ)) {

              $currentComments['replies'][] = array(
                  'id' => $obj->reply_id,
                  'reply_names' => $obj->reply_names,
                  'reply_date' => $obj->reply_date,
                  'reply_time' => $obj->reply_time,
                  'reply_img' => $obj->reply_img,
                  'reply' => $obj->reply
              );

          }
          array_push($json_response, $currentComments); //push the values in the array

      }
      return json_encode($json_response);

【问题讨论】:

    标签: php mysql json pdo


    【解决方案1】:

    不要覆盖 $stmt 和 $obj。像这样的:

          $q = "SELECT * FROM comments WHERE comments.news_id =:id";         
          //prepare
          $stmt = $this->DB_con->prepare($q);
    
          $r = "SELECT * FROM comments_reply WHERE comments_reply.comment_id =:id"; 
          //prepare
          $stmt2 = $this->DB_con->prepare($r);
    
          //execute with bound parameter
          $stmt->execute( array(':id'=> $id));
    
          //create an array
          $json_response = array(); 
    
          while($obj=$stmt->fetch(PDO::FETCH_OBJ)) {
    
              $currentComments = array(
                  'id' => $obj->comment_id,
                  'comment_names' => $obj->comment_names,
                  'comment_date' => $obj->comment_date,
                  'comment_time' => $obj->comment_time,
                  'comment_img' => $obj->comment_img,
                  'comments' => $obj->comments,
                  'replies' => array()
              );
    
              //
              $stmt2->execute( array(':id'=> $obj->comment_id) );
    
              while($obj2=$stmt2->fetch(PDO::FETCH_OBJ)) {
    
                  $currentComments['replies'][] = array(
                      'id' => $obj2->reply_id,
                      'reply_names' => $obj2->reply_names,
                      'reply_date' => $obj2->reply_date,
                      'reply_time' => $obj2->reply_time,
                      'reply_img' => $obj2->reply_img,
                      'reply' => $obj2->reply
                  );
    
              }
              array_push($json_response, $currentComments); //push the values in the array
    
          }
          return json_encode($json_response);
    

    【讨论】:

    • 似乎第一个while循环之间的语句没有执行,因为我仍然得到空数组[]返回,如果我注释掉这一行 //$json_response = array();它返回未定义的变量:json_response 错误
    • @AlexImenwo 发布你的数据库示例,一个回复和一个评论就足够了......
    猜你喜欢
    • 2015-07-21
    • 2019-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-24
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多