2014-02-28 44 views
2

問題很長,所以我不得不縮短它。使用json_encode從MySQL查詢返回JSON對象

無論如何,我目前有以下結果的表。

什麼我做的是以下內容:關聯到一個問題

    1. 查詢所有的答案,將其存儲到一個數組

    這是我當前的查詢後,對其進行編碼:

    $stmt = "SELECT questions.question_text, answers.answer_text 
        FROM  questions, answers, test 
        WHERE questions.question_id = answers.question_id 
        AND  questions.test_id =1"; 
    
    $result = $connection->query($stmt); 
    

    哪給出我:

    enter image description here

    這是PHP:

    $encode = array(); 
    
    while($row = mysqli_fetch_assoc($result)) { 
        $encode[] = $row; 
    } 
    
    echo json_encode($encode); 
    

    ,給了我這樣的輸出:

    [ 
        { 
         "question_text": "What is HTML?", 
         "answer_text": "HTML is a Hypertext Markup Language" 
        }, 
        { 
         "question_text": "What is HTML?", 
         "answer_text": "HTML is a Hypertext Markup Language" 
        }, 
        { 
         "question_text": "What is HTML?", 
         "answer_text": "HTML is a food" 
        }, 
        { 
         "question_text": "What is HTML?", 
         "answer_text": "HTML is a food" 
        }, 
        { 
         "question_text": "What is HTML?", 
         "answer_text": "HTML is an Asynchronous language" 
        }, 
        { 
         "question_text": "What is HTML?", 
         "answer_text": "HTML is an Asynchronous language" 
        }, 
        { 
         "question_text": "What is HTML?", 
         "answer_text": "HTML is a styling language" 
        }, 
        { 
         "question_text": "What is HTML?", 
         "answer_text": "HTML is a styling language" 
        } 
    ] 
    

    這是所需輸出與json_encode:

    "What is HTML?": { 
         "1": "HTML is a Hypertext Markup Language", 
         "2": "HTML is a food", 
         "3": "HTML is an Asynchronous language", 
         "4": "HTML is a styling language" 
        } 
    

    什麼我目前得到的是多單對象與內他們的答案,但總是與它的答案之一。我希望製作一個包含所有答案和代表對象的問題的單個對象。我真的希望這是有道理的。我的邏輯很可能是失敗的,所以請原諒我。

    我試着玩while循環,但我無法得到它的工作。有人能帶領我實現我想要的產出嗎?

    謝謝。

  • +0

    你的問題有唯一的ID嗎? – Maximus2012

    +3

    您所需的輸出既不是'json_encode'的功能,也不是mysql相關的功能。你必須自己在'$ encode'中重新組織你的對象。 – charlee

    +0

    是的,他們這樣做。 @ Maximus2012 –

    回答

    0

    我改變了我的查詢到以下幾點:

    SELECT DISTINCT questions.question_text, answers.answer_text 
        FROM  questions, answers, test 
        WHERE questions.question_id = answers.question_id 
        AND  questions.test_id = 
    

    while循環這樣的:

    while($row = mysqli_fetch_assoc($result)) { 
        $encode[$row['question_text']][] = $row['answer_text']; 
    } 
    

    這給了我這樣的:

    { 
        "What is HTML?": [ 
         "HTML is a Hypertext Markup Language", 
         "HTML is a food", 
         "HTML is an Asynchronous language", 
         "HTML is a styling language" 
        ] 
    } 
    

    我現在可以工作。

    9

    聽起來只是改變你正在構建陣列...

    $encode = array(); 
    
    while($row = mysqli_fetch_assoc($result)) { 
        $encode[$row['question _text']][] = $row['answer_text']; 
    } 
    
    echo json_encode($encode); 
    
    +2

    不完全是OP想要的,但這足以提供一個明確的方法。 – charlee

    +0

    tbh問題是如此漫長,我撇去:) – ficuscr

    +0

    這給了我一個錯誤。不輸出任何東西。我相信它應該。我甚至從「question_text」中刪除了空格,但沒有運氣。 –

    0

    更改該位:

    $encode = array(); 
    
    while($row = mysqli_fetch_assoc($result)) { 
        $encode[] = $row; 
    } 
    

    要(從1開始,我已經加入了$我,而不是隻推到編碼陣列的末尾):

    $encode = array(); 
    $i = 1; 
    while($row = mysqli_fetch_assoc($result)) { 
        $encode[$row['question_text']][$i] = $row['answer_text']; 
        $i++; 
    } 
    

    你應該沒問題。

    0

    你不需要使用PHP來做額外的處理。

    只需使用mysql Group By

    $stmt = "SELECT questions.question_text, answers.answer_text 
        FROM  questions, answers, test 
        WHERE questions.question_id = answers.question_id 
        AND  questions.test_id =1 
        GROUP BY questions.question_id;" 
    
    +0

    這隻返回一個問題。 –

    +0

    你能否簡單介紹一下你的表格結構 – Leo