Jump to content

Problem with multiple result sets


TOA

Recommended Posts

Hey guys, hoping you can help...

 

I'm trying to pull questions and answers for a quiz. I have it working in multiple selects, but am trying to figure out a way to do it in one.

 

I've tried Unions, Joins of all flavors and can't seem to get what I need. What's happening is that each question is being displayed for each answer, instead of all answers for each question.

 

For example

 

What's happening:

Question 1

Answer 1 for question 1

Question 1

Answer 2 for question 1

etc...

 

What I need is:

Question 1

Answer 1 for question1

Answer 2 for question 1

etc...

 

This is the sql that gets me the above:

SELECT Ques_Text, Ans_Text FROM Questions NATURAL JOIN Answers

 

Any help would be greatly appreciated,

 

Thanks

 

P.s if you need more info, just let me know, glad to get whatever is needed

Link to comment
https://forums.phpfreaks.com/topic/226363-problem-with-multiple-result-sets/
Share on other sites

the select that you are showing is the right one... important thing is how you DISPLAY the information... display is just a matter of simple logic....

 

Yes, I knew that. I just phrased it wrong, I guess, forgive me.

 

I've seen/give several examples answering the same question here before.... just search...

 

I've tried that, and google, which is why I asked here  :D. Maybe you can recommend a term to search for since the one's I'm trying aren't bringing any results?

:(  .... here is an small and very simple to caught fish...

 

   $sql = "SELECT Ques_Text, Ans_Text FROM Questions NATURAL JOIN Answers";
   
   $result = mysql_query($sql) or die("Query Error : " . mysql_error());
   
   $old_Ques = ''; // Variable to Hold the LAST DISPLAYED Quest Value
   
   while ($row = mysql_fetch_array($result)) {
      if ($old_Ques != $row['Ques_Text']) {   // Condition to DISPLAY or not the QUEST
        echo "<br /> Question : " . $row['Ques_Text'];
        $old_Ques = $row['Ques_Text'];
      }
      
      echo "<br />Ans : " . $row['Ans_Text']; // DISPLAY the remaining field(s)
   }

:(  .... here is an small and very simple to caught fish...

 

   $sql = "SELECT Ques_Text, Ans_Text FROM Questions NATURAL JOIN Answers";
   
   $result = mysql_query($sql) or die("Query Error : " . mysql_error());
   
   $old_Ques = ''; // Variable to Hold the LAST DISPLAYED Quest Value
   
   while ($row = mysql_fetch_array($result)) {
      if ($old_Ques != $row['Ques_Text']) {   // Condition to DISPLAY or not the QUEST
        echo "<br /> Question : " . $row['Ques_Text'];
        $old_Ques = $row['Ques_Text'];
      }
      
      echo "<br />Ans : " . $row['Ans_Text']; // DISPLAY the remaining field(s)
   }

 

Over and above my good man, all I needed was a term. Thank you for the head start. I was in the right area, just didn't have my if condition right I think.

 

I'll try this in just a few and report back. Thanks again

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.