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 8742007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:25:26+00:00 2026-06-13T11:25:26+00:00

I have 2 tables: questions, answers. questions has the fields: questionId, title, category, correctAnswerID,

  • 0

I have 2 tables: questions, answers.

  • questions has the fields: questionId, title, category,
    correctAnswerID, url
  • answers has the fields: answerId, questionId, text

I have this function:

$result = query("SELECT * FROM questions INNER JOIN answers ON questions.questionId=answers.questionId");

it returns the questions 4 times, and every time with different answer.
I want an array that contains an array of questions, and every question will has an array of related answers.

Every question has 4 possible answers, and every question has the correct answer ID.

Thanks in advance.

  • 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-06-13T11:25:27+00:00Added an answer on June 13, 2026 at 11:25 am

    MySQL cannot return such a multi-dimensional array natively, only a 2 dimensional array where level one is the rows, and level-two are the columns.

    You’ll have to iterate the result set and build the array yourself.

    In PDO, you’d do something like this:

    try {
        $pdo = new PDO("mysql:host=localhost;dbname=test", "root", "");
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
        $stmt = $pdo->query("SELECT * FROM questions INNER JOIN answers ON questions.questionId=answers.questionId");
    
        $result = array();
    
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            foreach ($row as $column => $value) {
                if ($column != "answerId") {
                    $result[$row["questionId"]][$column] = $value;
                }
            }
            $result[$row["questionId"]][$row["answerId"]] = $row["text"];
        }
    }
    catch (PDOException $e) {
        //Todo: Error Handling Here
    }
    

    Which gives me the following result (yours would probably look different):

    array (size=2)
      1 => 
        array (size=9)
          'questionId' => string '1' (length=1)
          'title' => string 'b' (length=1)
          'category' => string 'letters' (length=7)
          'correctAnswerID' => string '2' (length=1)
          'text' => string 'd' (length=1)
          1 => string 'a' (length=1)
          2 => string 'b' (length=1)
          3 => string 'c' (length=1)
          4 => string 'd' (length=1)
      2 => 
        array (size=9)
          'questionId' => string '2' (length=1)
          'title' => string 'd' (length=1)
          'category' => string 'letters' (length=7)
          'correctAnswerID' => string '8' (length=1)
          'text' => string 'd' (length=1)
          5 => string 'a' (length=1)
          6 => string 'b' (length=1)
          7 => string 'c' (length=1)
          8 => string 'd' (length=1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two Tables: Table 1: Questions : QuestionId NUMERIC Title TEXT Test Data
I have 2 mysql tables 1. questions: with the following columns: id, title, answer1,
i have four tables user-question contains two columns: questionID, userID, the questions that the
In my database table(whitelist_domain_data) I have the fields id,url,data The Url column has multiple
I have a table like this, with three fields: User | Question# | Answer
I have following tables questions -> id, question_data, user_id users -> id, fname, lname
I have a couple of questions. 1) Why cannot we see system tables (like
I'm so new to this that even the answers on questions similar to my
Table questions has the question id and title. Table posts has the id, question_id,
I have 3 tables in mySQL database. questions_table with these fields: question_id (unique ID

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.