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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:32:15+00:00 2026-05-12T00:32:15+00:00

I’m using MySQLi with PHP(5.2.4) MySQL(5.0.51a) on Ubuntu 8.04 LAMP. The relevant Db tables

  • 0

I’m using MySQLi with PHP(5.2.4) MySQL(5.0.51a) on Ubuntu 8.04 LAMP.

The relevant Db tables are below:

Questions
+----+------------------------------------------------------------------------------------------------------------+
| id | question                                                                                                   |
+----+------------------------------------------------------------------------------------------------------------+
|  1 | What is the correct rate of flow (in millilitres per minute) for a 14<abbr title="Gauge">G</abbr> cannula? |
|  2 | Which of the following drugs is not an anaesthetic  induction agent?                                       | 
+----+------------------------------------------------------------------------------------------------------------+

answers
+----+--------------------------------------------------------+
| id | answer                                                 |
+----+--------------------------------------------------------+
|  1 | 344<abbr title="millilitres per minute">ml/min</abbr>. | 
|  2 | 205<abbr title="millilitres per minute">ml/min</abbr>. | 
|  3 | 98<abbr title="millilitres per minute">ml/min</abbr>.  | 
|  4 | 60<abbr title="millilitres per minute">ml/min</abbr>.  | 
|  5 | Thiopental sodium                                      | 
|  6 | Propofol                                               | 
|  7 | Etomidate                                              | 
|  8 | Domperidone                                            | 
+----+--------------------------------------------------------+

a_lookup (to associate questions with answers)
+------+------+
| q_id | a_id |
+------+------+
|    1 |    1 | 
|    1 |    2 | 
|    1 |    3 | 
|    1 |    4 | 
|    2 |    5 | 
|    2 |    6 | 
|    2 |    7 | 
|    2 |    8 | 
+------+------+

I’m connecting to the Db with MySQLi, using the following (bearing in mind that I just edited out the real username, password and database name in favour of place-holder generics):

<?php

if (!isset($qtype) && !isset($submitted)) {

  /*
  =========================================================================
  = queries the database if no specific question-type selected  =
  =========================================================================
  */

  $mysqli = new mysqli("localhost", "username", "password", "db_name");
  if ($result = $mysqli->query(
    "SELECT questions.id as qid, 
      questions.question as question
    FROM
      questions;")) {

      while ($row = $result->fetch_object()) {
        $i = $row->qid;

        $q[$i][question] = $row->question;

        if ($answers = $mysqli->query(
          "SELECT answers.id, answers.answer FROM answers, a_lookup, questions WHERE answers.id=a_lookup.a_id AND '$i'=a_lookup.q_id;")) {

          while ($row = $answers->fetch_object()) {

            if (!isset($c)) {
              $c = 1;
            }
            else {
              $c = $c;
            }

            $q[$i][answers][$c] = $row->answer;

            $c++;
          }
        }
      }
    }

  $mysqli->close();
}
elseif (isset($qtype)) {

  // this part should hopefully be invoked when tags 'question-types' are supported

}

?>

This returns the questions as expected, the print_r is below:

Array
(
    [1] => Array
        (
            [question] => What is the correct rate of flow (in millilitres per minute) for a 14G cannula?
            [answers] => Array
                (
                    [1] => 344ml/min.
                    [2] => 205ml/min.
                    [3] => 98ml/min.
                    [4] => 60ml/min.
                    [5] => 344ml/min.
                    [6] => 205ml/min.
                    [7] => 98ml/min.
                    [8] => 60ml/min.
                )

        )

    [2] => Array
        (
            [question] => Which of the following drugs is not an anaesthetic  induction agent?
            [answers] => Array
                (
                    [9] => Thiopental sodium
                    [10] => Propofol
                    [11] => Etomidate
                    [12] => Domperidone
                    [13] => Thiopental sodium
                    [14] => Propofol
                    [15] => Etomidate
                    [16] => Domperidone
                )

        )

I’m just confused by my ending up with eight results, rather than the four I was expecting. I get the eight -as anyone that knows anything about databases would expect whether I use the mysql terminal client, or php’s mysql-api.

What am I doing wrong to create the duplicates?

  • 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-12T00:32:15+00:00Added an answer on May 12, 2026 at 12:32 am

    You get duplicate answers because your second sql query matches for either question id in the questions table:

    Try

     SELECT answers.id, answers.answer FROM answers, a_lookup, questions WHERE
     answers.id=a_lookup.a_id AND '$i'=a_lookup.q_id AND '$i' = questions.id;
    

    instead

    In any case. your multiple tables in FROM is not very easy to follow. Try to JOIN instead because then you do not forget the filter in a table since you have to give one in every join.

    • 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 using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
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 am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.