2012-02-08 74 views
2

內陣列結果我有3個表,PHP的MySQL的選擇,陣列結果

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 | 

我的目標是與各自的評論數據和likers數據得到tbl_photo照片數據一起。我想陣列的結構如下凡在我有了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的一個查詢做到這一點?如果沒有,有沒有其他的方式來做到這一點?感謝你們!

回答

4

由於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); 
+0

是的,這將是最好的。 – deed02392 2012-02-08 12:11:49

+0

嗨!謝謝!大幫人! – janusbalatbat 2012-02-08 12:17:04

1

您不能在一個查詢中在每行中沒有大量重複數據的情況下執行此操作。在三個查詢中,通過結果來執行此操作會更高效和更輕鬆。您將查詢照片表格,循環播放並在循環的每個迭代中執行兩個查詢,一個用於評論,另一個用於喜歡。

-1
$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); 
+0

嗨@garry焊接,將嘗試一個!謝謝! – janusbalatbat 2012-02-08 11:17:44