如何从表中的不同行获取多个结果?

时间:2016-11-05 10:05:40

标签: php mysql

我正试图从我的表中获取所有结果行'问题'测验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];

3 个答案:

答案 0 :(得分:2)

获得Quiz id的Use 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);

答案 1 :(得分:1)

添加:

while($row = mysqli_fetch_array($result2)) {
   echo $row["Type"]." ".$row["Question"]." ".$row["Answer "]."\n"; 
} 

答案 2 :(得分: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>;