0

In my quiz system I am trying to actually now make the page of the questions, but it shows nothing when I try to show the question rows and the answer rows to the page.

<?php
    $q_qselect = mysql_query("SELECT * FROM `questions`");
    $q_qnumrows = mysql_num_rows($q_select);
    for($i=0;$i<$q_qnumrows;$i++){

        $q_qselect = mysql_query("SELECT * FROM `questions` WHERE `id`='$i'");
        $q_aselect = mysql_query("SELECT * FROM `answers` WHERE `question_id`='$i'");

        $q = mysql_fetch_assoc($q_qselect);
        $a = mysql_fetch_assoc($q_aselect);

        echo $q['question'] . "<br />";
        echo $a['answer'] . "<br />";
    }
?>

And also, another question - how can I actually check that he selected the correct answer? (radio button near each answer) when the field in the answers table is correct?

Siamak Motlagh
  • 5,028
  • 7
  • 41
  • 65
Michael Naidis
  • 116
  • 2
  • 4
  • 15
  • [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – John Conde Jan 21 '13 at 14:54
  • But I am used to this.. what is the problem of using mysql_*? can you please help me with my question sir? – Michael Naidis Jan 21 '13 at 14:55
  • The reasoning can be found in the tutorial he provided in his comment. – Ryan Beaulieu Jan 21 '13 at 14:57
  • I will read it, but can someone please tell me why is no working? – Michael Naidis Jan 21 '13 at 14:58
  • Why are you querying th same information twice? You select all your questions then reselect each question individually inside your for loop. Using the old mysql_* functions couldn't you just use a while loop and use mysql_fetch_assoc – Ryan Beaulieu Jan 21 '13 at 14:59
  • Can you give me an example of the code Ryan Beaulieu? – Michael Naidis Jan 21 '13 at 15:01
  • Instead of using a for loop and having to do a select statement again inside your foor loop you could do something like this: while ($row = mysql_fetch_assoc($q_qselect)) { // for each row returned do something here } – Ryan Beaulieu Jan 21 '13 at 15:03
  • Now I have made a while loop and inside it another while loop for the answers, but it still doesn't seem to show up, I want the questions to be shown and under each questions it's multiple choice answers. – Michael Naidis Jan 21 '13 at 15:05
  • Why do you have two different select statements? Why not just do an inner join on the two tables. I am assuming these tables a relational by some sort of id. That way you don't need multiple while loops and select statements. – Ryan Beaulieu Jan 21 '13 at 15:09
  • I have a table for questions and table for answers, in the answers table there is a field called question_id which is the ID of the question in 'questions' table. What it does now it shows all the questions but doesn't show the answers below each question. – Michael Naidis Jan 21 '13 at 15:12

2 Answers2

0
<?php
$question = mysql_query("SELECT questions.*, answers.* FROM questions inner join answers on questions.qid=answers.id");

   while($row = mysql_fetch_array($question)) {
   echo $row['question_column_name_in_DB'].'<br />' .$row['answer_column_name_in_DB'].'<br />';
}

?>

change column names to their appropriate names. Please look into PDO's once you have gotten this to work.

Ryan Beaulieu
  • 453
  • 3
  • 15
  • how are you calling this php file? From another php file? If so did you remember to include this file ? – Ryan Beaulieu Jan 21 '13 at 15:23
  • exam.php, I don't include it from other file. – Michael Naidis Jan 21 '13 at 15:25
  • do me a favor. Comment out your code and just try echoing a string and let me know if it echoes out for you. If it does then it may mean you have a problem with your query. – Ryan Beaulieu Jan 21 '13 at 15:26
  • $question = mysql_query("SELECT questions.*, answers.* FROM questions inner join answers on questions.qid=answers.id"); while($row = mysql_fetch_assoc($question)) { echo $row['question'].'
    ' .$row['answer'].'
    '; } And I have manged to echo a string.
    – Michael Naidis Jan 21 '13 at 15:29
  • qid and answers.id were jsut id's i made up becuase I did not know your table structure. You should have an id in your answers table that is associated with the question in the questions table. That's the id that you want to join together on your tables. Whatever the column names are in each table is what you put there. – Ryan Beaulieu Jan 21 '13 at 15:31
  • Fixed it partly but now it does like: Question1 Answer1 Question1 Answer2 and repeatedly until it is out of answers. I want it to be: Question1 Answer1 Answer2 Answer3 Answer4 Question2 and so on.. – Michael Naidis Jan 21 '13 at 15:35
  • Post your code again. You must have a minor error in the code. Also if you could post what columns are in the questions and ansqers table. That way if there is a mySqul qury issue I can spot it. – Ryan Beaulieu Jan 21 '13 at 15:40
  • $question = mysql_query("SELECT questions.*, answers.* FROM questions inner join answers on questions.id=answers.question_id"); while($row = mysql_fetch_array($question)) { echo $row['question'].'
    ' .$row['answer'].'
    '; } The columns in questions table: id(AI) question_id(unusable meanwhile) question(varchar) type(unusable meanwhile) answers table: id(AI) question_id(int) answer correct(1=yes,0=no)
    – Michael Naidis Jan 21 '13 at 15:49
  • $question = mysql_query("SELECT questions.question, answers.answer FROM questions inner join answers on questions.id=answers.question_id"); while($row = mysql_fetch_array($question)) { echo $row['question'].'
    ' .$row['answer'].'
    '; }
    – Ryan Beaulieu Jan 21 '13 at 15:54
  • Also, double check your answer db to see if multiple answers have the same question id. That could throw off your results. – Ryan Beaulieu Jan 21 '13 at 15:55
  • Multiple answers have to have the same questionid, because they are part of the question. An answer that is a part of the question should have the same question_id as the `id` in `questions` table. The problem is that it shows the answers and the questions by order and every answers related to the question above, it justs duplicated the question after each answer. – Michael Naidis Jan 21 '13 at 15:57
  • if you have questions with multiple answers then you will get an output exactly how you are receiving it. Question 1 answer 1 question 1 answer 2 – Ryan Beaulieu Jan 21 '13 at 15:59
  • How can I have an output that it shows the question, and then the answers? and then the other question, and then the answers to that question and so on..? – Michael Naidis Jan 21 '13 at 16:00
0

You Can Try by using As Like Following....

<?php

  $query_result = mysql_query("SELECT questions.*, answers.* FROM questions LEFT JOIN answers on questions.id=answers.`question_id`");

  while($row = mysql_fetch_array($query_result)) {
    echo $row ['question'] . "<br />";
    echo $row ['answer'] . "<br />";
  }

?>