0

I want to encode an array of answers to a question when retrieving that question from the database.

Desired Result:

[{"question":"What Barks","answers":["Cats","Dogs","Birds","Elephants"],"correct":1}]

Source Code

require_once("connect.php");
$sql = "SELECT questions.question, GROUP_CONCAT(answers.answer) answers, questions.correct FROM questions,answers  where answers.questionID = questions.questionID group by questions.question";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    $data = array();
    while($row = mysqli_fetch_assoc($result)) {
        $data[] = $row;
    }
}

echo json_encode($data);

mysqli_close($conn);

Current Result:

[{"question":"What Barks?","answers":"Cat,Dog,Fish,Cow","correct":"1"}]
3
  • Try to chnage From $sql = "SELECT questions.question, questions.correct, answers.answer FROM questions INNER JOIN answers ON answers.questionID = questions.questionID"; TO $sql = "SELECT questions.question, questions.correct, answers.answer As answersFROM questions INNER JOIN answers ON answers.questionID = questions.questionID"; Commented Apr 12, 2018 at 6:52
  • 1
    You will need to format the array to suit your desired result. Right now you are just encoding the DB result. Commented Apr 12, 2018 at 6:52
  • I think your DB schema needs some work! Your data isn't even 1NF Commented Apr 12, 2018 at 9:31

2 Answers 2

1

Here is sample solution, based on example in your question:

require_once("connect.php");
$sql = 
    "SELECT questions.question, GROUP_CONCAT(answers.answer) answers, questions.correct FROM questions, answers WHERE answers.questionID = questions.questionID GROUP BY questions.question";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    $data = array();
    while($row = mysqli_fetch_assoc($result)) {
        $a = array(
            "question"=>$row["question"],
            "answers"=>explode(",", $row["answers"]),
            "correct"=>$row["correct"]
        );
        $data[] = $a;
    }
}
echo json_encode($data);
mysqli_close($conn);
Sign up to request clarification or add additional context in comments.

Comments

1

Change the mysql query as follows:

> SELECT questions.question, 
> questions.correct,GROUP_CONCAT(answers.answer) answers FROM
> questions,answers  where answers.questionID = questions.questionID
> group by questions.question

Here we use a GROUP_CONCAT aggregate function from MySQL

.

2 Comments

Thanks for reply, almost works as intended currently the result is producing [{"question":"What Barks?","answers":"Cat,Dog,Fish,Cow","correct":"1"}] How do I add it in to an array
Can you try this @Bradley if (mysqli_num_rows($result) > 0) { $data = array(); while($row = mysqli_fetch_assoc($result)) { $answers = $row["answers"]; $row["answers"] = explode(',', $answers); $data[] = $row; } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.