【问题标题】:php mysql select, array result inside array resultphp mysql select,数组结果内的数组结果
【发布时间】:2012-02-29 19:22:48
【问题描述】:

我有 3 张桌子,

 tbl_photo               tbl_photo_comments      tbl_photo_likers
 ___________             ____________             _____________
| photo_id  |           | comment_id |           | like_id     |
| photo_url |           | photo_id FK|           | user_id FK  |
| user_id FK|           | user_id FK |           | photo_id FK |
                        | comment    | 

我的目标是从 tbl_photo 中获取照片数据以及它们各自的 cmets 数据和 likers 数据。我想要的数组结构如下,其中我有一个结果数组,其中有 2 个数组作为其数据的元素

oneResultArray =
{
  photo_url = "www.url.com/photo.png";
  photoID = 1;
  user_id =  2
  commentData = (
     {
         comment = "comment 1";
         userid = 1
     },
     {
         comment = "comment 2";
         userid = 2
     },
     {
         comment = "comment 3";
         userid = 3});

     likersData = (
     {
         userid = 2;
         username = liker1;
     },
     {
         userid = 3;
         username = liker2;
     });

   },
{
  photo_url = "www.url.com/photo.png";
  photoID = 1;
  user_id =  2
  commentData = (
     {
         comment = "comment 1";
         userid = 1
     },
     {
         comment = "comment 2";
         userid = 2
     },
     {
         comment = "comment 3";
         userid = 3});

     likersData = (
     {
         userid = 2;
         username = liker1;
     },
     {
         userid = 3;
         username = liker2;
     });
   }

我的问题是,是否可以在 mysql 上使用一个查询来完成此操作?如果没有,有没有其他方法可以做到这一点?谢谢你们!

【问题讨论】:

    标签: php mysql multidimensional-array


    【解决方案1】:

    正如 davidethell 指出的那样,您不想加入这些表格。因此,不可能在单个查询中选择您的数据。 Garry Welding 的方法可以解释为对您拥有的每张照片记录运行后续查询。这也不是您想要做的不是。 3 张照片将导致执行 7 个查询。这比必要的多4个。 10 张照片将导致 21 个查询。你得到了图片。尝试以下方式:

    <?php
    
    // build hierarchical result
    $result = array();
    
    // getting the photos
    $query = $pdo->query('SELECT photo_id, photo_url, user_id FROM tbl_photo WHERE user_id = 5');
    foreach ($query as $row) {
        $result[$row['photo_id']] = $row;
        $result[$row['photo_id']]['comments'] = array();
        $result[$row['photo_id']]['likes'] = array();
    }
    
    if ($result) {
        // comments and likes only for the photos we've selected
        $photos = join(',', array_keys($result));
    
        // getting the comments
        $query = $pdo->query('SELECT comment_id, photo_id, user_id, comment FROM tbl_photo_comments WHERE photo_id IN (' . $photos . ')');
        foreach ($query as $row) {
            $result[$row['photo_id']]['comments'][$row['comment_id']] = $row;
        }
    
        // getting the likes
        $query = $pdo->query('SELECT like_id, user_id, photo_id FROM tbl_photo_likers WHERE photo_id IN (' . $photos . ')');
        foreach ($query as $row) {
            $result[$row['photo_id']]['likes'][$row['like_id']] = $row;
        }
    }
    
    var_dump($result);
    

    【讨论】:

      【解决方案2】:

      如果每一行中没有大量重复数据,您就无法在一个查询中执行此操作。在三个查询中遍历结果将更有效、更容易。您将查询照片表,循环遍历它,并在循环的每次迭代中执行两个查询,一个用于 cmets,另一个用于 likes。

      【讨论】:

        【解决方案3】:
        $photos = {code to get the array of photos};
        
        foreach($photos as &$photo) {
            $photo['comments'] = {code to get photo comments using the photo id};
            $photo['likes'] = {code to get photo likes using the photo id}
        }
        
        return $photos;
        

        这将是我所知道的唯一方法。 $photo 之前的 & 是通过引用传递数组。通过引用传递数组仅在 PHP5+ 中可用,http://php.net/manual/en/language.types.array.php

        来自上面的 PHP.net 链接:

        // PHP 5
        foreach ($colors as &$color) {
            $color = strtoupper($color);
        }
        unset($color); /* ensure that following writes to
        $color will not modify the last array element */
        
        // Workaround for older versions
        foreach ($colors as $key => $color) {
            $colors[$key] = strtoupper($color);
        }
        
        print_r($colors);
        

        【讨论】:

          猜你喜欢
          • 2019-03-13
          • 1970-01-01
          • 1970-01-01
          • 2021-12-14
          • 2015-09-03
          • 1970-01-01
          • 2019-01-26
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多