Php MySQL的基本JSON输出

Php MySQL的基本JSON输出,php,mysql,json,Php,Mysql,Json,如何修改代码MySQL以输出此JSON数据: { "Monday": [ { "title": "Title 1 goes here", "details": "Lesson Details here...." }, { "title": "Title 2 goes here", "details": "Lesson Details here..." } ], "Tuesday": [ {

如何修改代码MySQL以输出此JSON数据:

{
"Monday": [
    {
        "title": "Title 1 goes here",
        "details": "Lesson Details here...."
    },
    {
        "title": "Title 2 goes here",
        "details": "Lesson Details here..."
    }
],
"Tuesday": [
    {
        "title": "Title 3 goes here",
        "details": "Lesson Details here..."
    },
    {
        "title": "Title 4 goes here",
        "details": "Lesson Details here..."
    }
],
"Wednesday": [
    {
        "title": "Title 5 goes here",
        "details": "Lesson Details here..."
    },
    {
        "title": "Title 6 goes here",
        "details": "Lesson Details here..."
    }
]
}

MySQL代码如下: 我使用数据库中的视图来输出带课程的天数,每天都有很多课程

            $query = "SELECT * FROM LessonsView";
        $resultset = mysql_query($query, $connection);
        $records = array();

        //Loop through all our records and add them to our array
        while($r = mysql_fetch_assoc($resultset))
        {
            $records['LessonsData'][] = $r; 
        }

        //Output the data as JSON
        header('Content-type: application/json');
        echo json_encode($records);
    }
}
我非常感谢您对我的代码的帮助,因为我是MYSQL新手。 谢谢

这完全取决于数据库中的列。很可能你有一个标题和一个细节栏。我假设你有某种列来存储工作日的某个地方

退房


目前的输出是什么?到目前为止,您尝试了什么来实现这一点?我看不出您在哪里尝试将一周中的几天作为对象属性或关联键数组写入任何位置。同样,如果不了解表模式或包含的数据,就不可能回答这个问题。您现在向我们展示的是
SELECT*
,它没有告诉我们任何事情(通常不应该顺便使用)。
SELECT * FROM LessonsView
SELECT Weekday, title, details FROM LessonsView GROUP BY Weekday, title, details