我得到這個JSON結果從MySQL用下面的PHP查詢:PHP MySQL查詢組由項目
/* grab the posts from the db */
$query = "SELECT * FROM ko_timetable";
$result = mysql_query($query,$link) or die('Errant query: '.$query);
/* create one master array of the records */
$posts = array();
if(mysql_num_rows($result)) {
while($post = mysql_fetch_assoc($result)) {
$posts[] = array('post'=>$post);
}
}
JSON結果:
post = {
"CLASS_LEVEL" = "Intro/General";
"CLASS_TYPE" = "Muay Thai";
"DAY_OF_WEEK" = Sunday;
ID = 19;
"ORDER_BY" = 5;
TIME = "1:00pm - 2:30pm";
};
}
{
post = {
"CLASS_LEVEL" = "General/Intermediate/Advanced";
"CLASS_TYPE" = "Muay Thai Spar - Competitive";
"DAY_OF_WEEK" = Sunday;
ID = 27;
"ORDER_BY" = 5;
TIME = "6:00pm - 9:00pm";
};
},
{
post = {
"CLASS_LEVEL" = "Fighters/Advanced/Intermediate";
"CLASS_TYPE" = "Fighters Training";
"DAY_OF_WEEK" = Monday;
ID = 1;
"ORDER_BY" = 1;
TIME = "9:30am - 11:00pm";
};
但我怎麼能更改此查詢以「DAY_OF_WEEK」獲得此結果組。見下面的例子,感謝您的幫助:
{
Sunday = {
"CLASS_LEVEL" = "Intro/General";
"CLASS_TYPE" = "Muay Thai";
"DAY_OF_WEEK" = Sunday;
ID = 19;
"ORDER_BY" = 5;
TIME = "1:00pm - 2:30pm";
};
{
"CLASS_LEVEL" = "General/Intermediate/Advanced";
"CLASS_TYPE" = "Muay Thai Spar - Competitive";
"DAY_OF_WEEK" = Sunday;
ID = 27;
"ORDER_BY" = 5;
TIME = "6:00pm - 9:00pm";
};
},
{
Monday = {
"CLASS_LEVEL" = "Fighters/Advanced/Intermediate";
"CLASS_TYPE" = "Fighters Training";
"DAY_OF_WEEK" = Monday;
ID = 1;
"ORDER_BY" = 1;
TIME = "9:30am - 11:00pm";
};
感謝
請不要使用古老的'mysql_ *'函數編寫新代碼。他們不再被維護,社區已經開始[棄用流程](http://news.php.net/php.internals/53799)。相反,您應該瞭解準備好的語句並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/mysqli)。如果你關心學習,[這裏是一個很好的PDO相關教程](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers)。 –
另外,無論這是什麼,它不是JSON。 –
@Truth,問題是**如何更改此查詢以通過「DAY_OF_WEEK」獲取此結果組?** ... –