0

私は 2 つの mysql データベース テーブルを持っています。1 つは投稿用で、もう 1 つはコメント用です。

ポストテーブル

+----+-------+
| ID | texts |
+----+-------+
| 1  | abc   |
| 2  | xyz   |
+----+-------+

そしてコメント表

+----+--------+-------+
| ID | postid | texts |
+----+--------+-------+
| 1  | 1      | abc1  |
| 2  | 1      | abc2  |
| 3  | 1      | abc3  |
| 4  | 2      | xyz1  |
| 5  | 2      | xyz2  |
+----+--------+-------+

さて、最小限のmysqlクエリリクエストで投稿を取得する方法。出力は次のようになります。

$data = array(
    0 => array(
        ID => 1,
        texts => abc,
        comments => array(
            0 => array(
                ID => 1,
                texts => abc1
            )
            1 => array(
                ID => 2,
                texts => abc2
            )
            2 => array(
                ID => 3,
                texts => abc3
            )
        )
    )
    1 => array(
        ID => 2,
        texts => xyz,
        comments => array(
            0 => array(
                ID => 4,
                texts => xyz1
            )
            1 => array(
                ID => 5,
                texts => xyz2
            )
        )
    )
)
4

2 に答える 2

2

どうですか

SELECT  *
FROM    Post p LEFT JOIN
        Comments c  ON  p.ID = c.postID
于 2013-08-19T12:57:26.160 に答える
1

結果を配列に入れるコードを提供していただけると助かります

最初に、より使いやすい多次元配列をお勧めします。

配列形式:

$data = array(
    post.ID => array(
        "texts" => post.texts,
        "comments" => array(
            comments.ID => comments.texts,
        ),
    ),
);

上記の形式は、特に配列への直接アクセスや foreach ループでの作業が容易になります。

mysqli_*関数と while ループを使用して、MySQL の結果から配列にデータを代入するには、次の手順を実行します。

//connect to mysql database
$link = $mysqli_connect("localhost","your_user","your_password","your_database");
//form mysql query
$query = "
    SELECT
        post.ID AS post_id,
        post.texts AS post_texts,
        comments.ID AS comments_id,
        comments.texts AS comments_texts
    FROM
        post
        LEFT JOIN comments ON (comments.postid = post.ID)
    WHERE
        posts.ID < 10
";
//run mysql query and return results
$mysqli_result = mysqli_query($link,$query);
//define empty $data array
$data = array();
//loop through result sets fetching string array with each result row
while($row = mysqli_fetch_array($mysqli_result)){
    //set the post text if not already set
    if(!isset($data[$row["post_id"]]["texts"])){
        $data[$row["post_id"]]["texts"] = $row["post_texts"];
    }
    //set the comments data if not NULL otherwise set comments to empty array to maintain structure
    if(!empty($row["comments_id"])){
        $data[$row["post_id"]]["comments"][$row["comments_id"]] = $row["comments_texts"];
    } else {
        $data[$row["post_id"]]["comments"] = array();
    }
}
//free the results set
mysqli_free_result($mysqli_result);
//close connection to mysql database
mysqli_close($link);

//print out the post text with the id of 1 with two line breaks
//be careful using this method unless you are sure that post with id of 1 exists or first check if(isset($data["1"])){...}
print $data["1"]["texts"]."<br /><br />";

//loop through all of the comments for a particular post with id of 1
foreach($data["1"]["comments"] as $key => $value){
    //print out the comment id with a line break
    print "Comment ID: ".$key."<br />";
    //print out the comments texts with two line breaks
    print "Comment: ".$value."<br /><br />";
}

//loop through and print all the post texts and how many comments exist for the post
foreach($data as $key => $value){
    //print the post ID with a line break
    print "Post ID: ".$key."<br />";
    //print the post texts with a line break
    print "Post: ".$value["texts"]."<br />";
    //count the number of comments
    $num_comments = count($value["comments"]);
    //get correct plural form of noun
    ($num_comments==1) ? $comments = "comment" : $comments = "comments";
    //print the number of comments for the post with two line breaks
    print $num_comments." ".$comments." for this post.<br /><br />";
}
于 2013-08-20T15:31:57.967 に答える