Reputation: 15
I am trying to get all the rows of results from my table 'question' where the quiz ID ($currentQuizID) is the same. My code only allows me to get a single row of data from the table, so what should I do?
$findQuiz = "SELECT QuizID FROM quiz ORDER BY QuizID DESC";
$result = mysqli_query($user->db,$findQuiz);
$iddata = mysqli_fetch_array($result);
$currentQuizID = $iddata['QuizID'];
$findQuestions = "SELECT Type, Question, Answer FROM question WHERE QuizID = '".$currentQuizID."'";
$result2 = mysqli_query($user->db,$findQuestions);
$iddata2 = mysqli_fetch_array($result2);
echo $iddata2[0];
echo $iddata2[1];
echo $iddata2[2];
Upvotes: 0
Views: 74
Reputation: 386
After getting Quiz id's Use while loop to get multiple records from question's table
$findQuiz = "SELECT QuizID FROM quiz ORDER BY QuizID DESC";
$result = mysqli_query($user->db,$findQuiz)
$totalArray = array();
while($iddata = mysqli_fetch_array($result);)
{
$currentQuizID = $iddata['QuizID'];
$findQuestions = "SELECT Type, Question, Answer FROM question WHERE QuizID = '".$currentQuizID."'";
$result2 = mysqli_query($user->db,$findQuestions);
$iddata2 = mysqli_fetch_array($result2);
$res['Type'] = $iddata2[0];
$res['Question'] = $iddata2[1];
$res['Answer'] = $iddata2[2];
array_push($totalArray,$res);
}
print_r($totalArray);
Upvotes: 1
Reputation: 490
$findQuestions = "SELECT Type, Question, Answer FROM question WHERE QuizID = '".$currentQuizID."'";
$result2 = mysqli_query($user->db,$findQuestions);
$iddata_questions = array();
while($row = mysqli_fetch_array($result2)) {
$iddata_questions[] = array($row["Type"],
$row["Question"],
$row["Answer"]);
}
echo "<pre>";
var_dump($iddata_questions);
echo "</pre>;
Upvotes: 0
Reputation: 5039
Add this:
while($row = mysqli_fetch_array($result2)) {
echo $row["Type"]." ".$row["Question"]." ".$row["Answer "]."\n";
}
Upvotes: 2