JSON对象中的两个MySQL查询

时间:2022-10-18 21:42:55

I am trying to build the following JSON through MySQL and PHP.
Each Chapter has an id, a chapter title, and a list of topics.
Each topic has an id and a topic area

我试图通过MySQL和PHP构建以下JSON。每章都有一个id,一个章节标题和一个主题列表。每个主题都有一个id和一个主题区域

{
"Chapters": [
    {
        "id": "1",
        "chapterTitle": "Introduction",
        "Topics": [
            {
                "id": "1",
                "topicArea": "C++"
            },
            {
                "id": "2",
                "topicArea": "Java"
            }
        ]
    },
    {
        "id": "2",
        "chapterTitle": "Getting Started",
        "Topics": [
            {
                "id": "1",
                "topicArea": "Start Here"
            }
        ]
    }
 ]
}

However, I am not able to generate this output if I try the following PHP code (1)

但是,如果我尝试以下PHP代码,我将无法生成此输出(1)

//Get all chapters
$result = mysql_query("SELECT * FROM chapters");      

while ($row = mysql_fetch_array($result))
{
    $json[] = $row;
    $json2 = array();
    $chapterid = $row["id"];

    //Fetch all topics within the first book chapter
    $fetch = mysql_query("SELECT id,topicArea FROM topics where chapterid=$chapterid");
    while ($row2 = mysql_fetch_assoc($fetch))
    {
         $json2[] = array( 
            'id' => $row2["id"],
            'topicArea' => $row2["topicArea"]
           );
     }
     $json['Topics'] = $json2;          //I think the problem is here!
}
echo json_encode($json);

1 个解决方案

#1


1  

Please don't use the mysql_* functions any more. Both mysqli and PDO enables you to use prepared statements.

请不要再使用mysql_ *函数了。 mysqli和PDO都允许您使用预准备语句。

With that said, you are right that $json['Topics'] = $json2; is the problem: this needs to be $json['Topics'][] = $json2;.

有了这个说,你是对的$ json ['Topics'] = $ json2;问题是:这需要是$ json ['Topics'] [] = $ json2;。

Finally for performance reasons (look at What is SELECT N+1?), you might want to look into a JOIN. All in all:

最后由于性能原因(看看什么是SELECT N + 1?),您可能想要查看JOIN。总而言之:

$dbh = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
$query = 'SELECT c.id AS chapter_id, c.chapterTitle, t.id as topic_id, t.topicArea FROM chapters c INNER JOIN topics t ON c.id = t.chapterid';
$json = array();
foreach ($dbh->query($query) as $row) {
  $json[$row['chapter_id']]['id'] = $row->chapter_id;
  $json[$row['chapter_id']]['chapter_title'] = $row->chapter_title;
  $json[$row['chapter_id']]['Topics'][] = array(
    'id' => $row->topic_id,
    'topicArea' => $row->topicArea,
  );
}
print json_encode(array_values($json));

#1


1  

Please don't use the mysql_* functions any more. Both mysqli and PDO enables you to use prepared statements.

请不要再使用mysql_ *函数了。 mysqli和PDO都允许您使用预准备语句。

With that said, you are right that $json['Topics'] = $json2; is the problem: this needs to be $json['Topics'][] = $json2;.

有了这个说,你是对的$ json ['Topics'] = $ json2;问题是:这需要是$ json ['Topics'] [] = $ json2;。

Finally for performance reasons (look at What is SELECT N+1?), you might want to look into a JOIN. All in all:

最后由于性能原因(看看什么是SELECT N + 1?),您可能想要查看JOIN。总而言之:

$dbh = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
$query = 'SELECT c.id AS chapter_id, c.chapterTitle, t.id as topic_id, t.topicArea FROM chapters c INNER JOIN topics t ON c.id = t.chapterid';
$json = array();
foreach ($dbh->query($query) as $row) {
  $json[$row['chapter_id']]['id'] = $row->chapter_id;
  $json[$row['chapter_id']]['chapter_title'] = $row->chapter_title;
  $json[$row['chapter_id']]['Topics'][] = array(
    'id' => $row->topic_id,
    'topicArea' => $row->topicArea,
  );
}
print json_encode(array_values($json));