Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7042113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:07:44+00:00 2026-05-28T02:07:44+00:00

I’m just starting to learn more advanced SQL along with PHP and I’m really

  • 0

I’m just starting to learn more advanced SQL along with PHP and I’m really struggling trying to find out how to query my database for a quiz I’m building.

Ultimately, I’m trying to return a json object with the following structure which gives me a list of questions and all possible answers as a multidimensional array:

{
    "questions":
        [
            {
                "question": "question text here",
                "answers":
                    [
                         { "answer": "answer text here", "points": 10 },
                         { "answer": "answer text here", "points": 20 },
                         { "answer": "answer text here", "points": 30 },
                         { "answer": "answer text here", "points": 40 }
                    ]
            },
            {
                "question": "question text here",
                "answers":
                    [
                         { "answer": "answer text here", "points": 10 },
                         { "answer": "answer text here", "points": 20 },
                         { "answer": "answer text here", "points": 30 },
                         { "answer": "answer text here", "points": 40 }
                    ]
            }
        ]
{

…from my mySQL tables of the following structure:

quiz

id | title
1  | quiz title here

quiz_question

id | quiz_id (FK) | question_text
1  |       1      | question text here
2  |       1      | question text here

quiz_answer

id | quiz_question_id (FK) | answer_text      | points
1  |            1          | answer text here |   10
2  |            1          | answer text here |   20
3  |            1          | answer text here |   30
4  |            1          | answer text here |   40

…with the following foreign keys:

quiz_question.quiz_id is FK to quiz.id
quiz_answer.quiz_question_id is FK to quiz_question.quiz_id

…using the following PHP (in it’s simplest form which is currently only returning my questions):

//query the db
$query = mysql_query("
    SELECT quiz_question.question_text
    FROM quiz_question
    JOIN quiz ON quiz.id = quiz_question.quiz_id
    WHERE quiz.id = 1;
");

$numrows = mysql_num_rows($query);
for ($i = 0; $i < $numrows; $i++) {
    $row = mysql_fetch_assoc($query);
    $quiz_data[$i] = array("question" => $row["question_text"]);
}

//echo JSON to page
$response = $_GET["jsoncallback"] . "(" . json_encode($quiz_data) . ")";
echo $response;

…and using jQuery’s $.getJSON() in my JavaScript which gets my a JSON formatted object from my PHP which gets me back the following:

[
    {"question":"question text here"},
    {"question":"question text here"}
]

So my question is, how can I write my SQL and PHP to create a multidimensional array like the very above instead of a single array like I’m currently getting back now? I need to figure out how to include the questions and all associated answers as a multidimensional array.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:07:44+00:00Added an answer on May 28, 2026 at 2:07 am

    You can’t retrieve a multi-dimensional array purely with mysql (at least as far as I know). You will have to do some php processing. This doesn’t sound too crazy.

    First, update your query to select answers at the same time by joining quiz_answers on quiz_questions using the question ID. Then, in your loop:

    $quiz = array();
    while ($row = mysql_fetch_assoc($result)) {
       // you don't need to check num_rows
       // fetch_assoc returns false after the last row, so you can do this
       // which is cleaner
       if (!isset($quiz[$row['question_id'])) {
          $quiz[$row['question_id']] = array(
             'question' => $row['question_text']
             , 'answers' => array()
          );
       }
       $quiz[$row['question_id']]['answers'][] = $row['answer_text'];
    }
    $full = json_encode(array('questions' => $quiz'));
    

    This will give you the array you want after it’s json encoded.

    Note that you will end up selecting the question text/id once per each answer, which is inefficient. You can use GROUP_CONCAT on the answers, but the above will still work almost identically, you just have to split the answer string.

    I also suggest you use PDO or some other wrapper over mysql_*.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.