2016-11-05 74 views
0

我試圖從我的表'問題'中測驗ID($ currentQuizID)是相同的結果的所有行。我的代碼只允許我從表中獲取單行數據,那麼我該怎麼做?如何從表中的不同行獲取多個結果?

$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]; 
+0

它們放在一個while循環。 –

+0

while($ row = mysqli_fetch_array($ result2)){ $ questions [] = $ rows ['type']; } add while循環獲取多條記錄。 –

+0

@ L.Herrera我會如何寫它? –

回答

1

補充一點:

while($row = mysqli_fetch_array($result2)) { 
    echo $row["Type"]." ".$row["Question"]." ".$row["Answer "]."\n"; 
} 
0
$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>; 
2

越來越測驗ID的使用後while循環,從問題的表中獲取的多條記錄

$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); 
相關問題