Jump to content

sql statement help


woodplease

Recommended Posts

I'm creating a quiz script so that a user can enter a quiz name, and then add questions and answers to that quiz.

 

I have an SQL statement where I join 2 tables (questions and answers), and which is part of a query that echoes out criteria that matches the SQL. I need to change the SQL statement so that it also only prints out the data if it matches data in a column from the table holding the quiz name, i.e it only prints out the questions and answers for that particular quiz.

 

This is what I thought it would be, but it doesn't work. It keeps printing out the answers from all of the quizes.

 

$query2 = "SELECT questions.questionno, questions.question, questions.quizref, questions.questionref, answers.answerno, answers.answer, answers.answervalue, answers.questionno, answers.quizref, answers.answerref FROM questions JOIN answers ON questions.questionno = answers.questionno WHERE questions.questionno = ".$row['questionno']. "AND answers.quizref = ".$id;

 

Any help would be great

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/190449-sql-statement-help/
Share on other sites

i've tried adding another join statement, but its not working. any ideas?

$query2 = "SELECT questions.questionno, questions.question, questions.quizref, questions.questionref, answers.answerno, answers.answer, answers.answervalue, answers.questionno, answers.quizref, answers.answerref, quiz.quizref, quiz.name, quiz.createdby FROM questions 
JOIN answers ON questions.questionno = answers.questionno 
WHERE questions.questionno = ".$row['questionno']. "JOIN answers ON quiz.quizref = answers.quizref WHERE quiz.quizref = ".$id;

Link to comment
https://forums.phpfreaks.com/topic/190449-sql-statement-help/#findComment-1004641
Share on other sites

You have to do the joins prior to the where, you have them mixed in.

 

I am not sure if my syntax is right but give this a try:

 

   $query2 = "SELECT questions.questionno, questions.question, questions.quizref, questions.questionref, answers.answerno, answers.answer, answers.answervalue, answers.questionno, answers.quizref, answers.answerref, quiz.quizref, quiz.name, quiz.createdby FROM questions
   JOIN answers ON questions.questionno = answers.questionno JOIN quiz ON quiz.quizref = answers.quizref
   WHERE questions.questionno = ".$row['questionno']. " AND quiz.quizref = ".$id;

 

 

Link to comment
https://forums.phpfreaks.com/topic/190449-sql-statement-help/#findComment-1004642
Share on other sites

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.