简体   繁体   中英

PDO Query from Results of Another PDO Query

I have this code which hits a view in mysql called vAlbums this returns a JSON array of the query results

function getAlbumPics($arno){
    $aSql = "SELECT albu_ablumid, albu_name_en, albu_name_de, albu_name_fr, albu_name_nl, albu_name_es, albu_name_it, albu_photourl FROM vAlbums WHERE site_arno=:arno";
    try {
        $db = getConnection();
        $aStmt = $db->prepare($aSql);
        $aStmt->bindParam(":arno",$arno);
        $aStmt->execute();
        $albums = $aStmt->fetchAll(PDO::FETCH_OBJ);
        $arrAID = $aStmt->fetchColumn(2);
        $db = null;
        echo '{"albums": ' . json_encode($albums) . '}';
    } catch(PDOException $e) {
        echo '{"error":[{"text":"'. $e->getMessage() .'"}],';
        echo '"SQL": ' . json_encode($aSql) .'}';
    }
}

I need to do a sub query to place an array of photos in each album in the array like thus

{
    "albums": [
        {
            "albu_ablumid": "1",
            "photos": [
                {
                    "photourl": "photo1"
                },
                {
                    "photourl": "photo2"
                },
                {
                    "photourl": "photo3"
                }
            ]
        },
        {
            "albu_ablumid": "2",
            "photos": [
                {
                    "photourl": "photo1"
                },
                {
                    "photourl": "photo2"
                },
                {
                    "photourl": "photo3"
                }
            ]
        }
    ]
}

Can someone show how to achieve this the MySQL query for photos is:

SELECT * FROM photos WHERE album_id = x

Thanks

Why not just do a join and group concat in a single query?

SELECT 
    albu_ablumid, 
    albu_name_en, 
    albu_name_de, 
    albu_name_fr, 
    albu_name_nl, 
    albu_name_es, 
    albu_name_it, 
    albu_photourl,
    group_concat(photourl) // Guessing at column Name
FROM 
    vAlbums a
        join photos b
            on a.albu_ablumid=b.album_id
WHERE 
    site_arno=:arno
group by
    albu_ablumid, 
    albu_name_en, 
    albu_name_de, 
    albu_name_fr, 
    albu_name_nl, 
    albu_name_es, 
    albu_name_it, 
    albu_photourl

This will return all the photos in a comma separated string in each row that matches the album. You can then easily split it up as needed in your code. It saves a lot of connections and multiple queries being run.

Here is a little example from my play db to show how it functions:

mysql> select * from table1;
+---------+------+------+-------------+
| autonum | ID   | name | metavalue   |
+---------+------+------+-------------+
|       1 |    1 | Rose | Drinker     |
|       2 |    1 | Rose | Nice Person |
|       3 |    1 | Rose | Runner      |
|       4 |    2 | Gary | Player      |
|       5 |    2 | Gary | Funny       |
|       6 |    2 | Gary | NULL        |
|       7 |    2 | Gary | Smelly      |
+---------+------+------+-------------+
7 rows in set (0.00 sec)

mysql> select ID, group_concat(metavalue) from table1 group by ID;
+------+----------------------------+
| ID   | group_concat(metavalue)    |
+------+----------------------------+
|    1 | Drinker,Nice Person,Runner |
|    2 | Player,Funny,Smelly        |
+------+----------------------------+
2 rows in set (0.00 sec)

and with some simple code to take care of the NULLs if you need to for some reason:

mysql> select ID, group_concat(coalesce(metavalue,'Empty')) from table1 group by ID;
+------+-------------------------------------------+
| ID   | group_concat(coalesce(metavalue,'Empty')) |
+------+-------------------------------------------+
|    1 | Drinker,Nice Person,Runner                |
|    2 | Player,Funny,Empty,Smelly                 |
+------+-------------------------------------------+
2 rows in set (0.00 sec)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM