变量数组只填充mysql表的最后一行

时间:2012-07-06 14:32:39

标签: php

我正在回复AnswerStatusID和AnswerResponse的结果,并且需要将它们应用于变量进行比较,以查看给出的答案是正确还是错误以及答案的相应响应,我的问题是我的变量只是由表中的最后一行填充,而不是用所有数据填充它。

// Connect to the Database
require_once('mysqli_connect.php');

//create the query for the question
$q = "SELECT `Question` FROM tbl_Question WHERE QuestionID = 1";

//Create the query for the Answers
$q2 = "SELECT `Answer`,`AnswerStatusID`,`AnswerResponse` FROM tbl_Answer WHERE QuestionID = 1";


//Run the query
$r = mysqli_query($conn,$q);

//run the answer query
$r2 = mysqli_query($conn,$q2);


while($row = mysqli_fetch_array($r,MYSQLI_ASSOC)){
echo '<div id="Question1"><p>1) ' . $row['Question'] . '</div></p>';
}



while($row2 = mysqli_fetch_array($r2,MYSQLI_ASSOC)){
echo '<div id="Question1"><input name="q1" type="radio" value="'.$AnswerStatusID.'"/>' . $row2['Answer'] . '</div><br/>';

//Assign the AnswerStatusID to a var
$AnswerStatusID = $row2['AnswerStatusID'];

//Assign the AnswerResponse to a var
$AnswerResponse = $row2['AnswerResponse'];
}

1 个答案:

答案 0 :(得分:2)

正如Wiseguy暗示的那样,看起来你想要AnswerStatusID和AnswerResponse作为数组。

这里有两个步骤。首先将它们声明为数组。

$AnswerResponse = array();
$AnswerStatusID = array();

然后将值存储在每个值中

while($row2 = mysqli_fetch_array($r2,MYSQLI_ASSOC)){
    echo '<div id="Question1"><input name="q1" type="radio" value="'.$AnswerStatusID.'"/>' . $row2['Answer'] . '</div><br/>';

    //Assign the AnswerStatusID to a var
    $AnswerStatusID[] = $row2['AnswerStatusID'];

    //Assign the AnswerResponse to a var
    $AnswerResponse[] = $row2['AnswerResponse'];
}

然后,您可以使用var_dump();

查看数组中的内容
var_dump($AnswerResponse)

print_r($AnswerResponse)