0
我正在從MySQL數據庫檢索值,並將它們填充到單選按鈕供用戶選擇。這些包裝在一個div中,動態填充問題。出於某種原因,它填充了所有與問題相關的答案,但第二到第四個答案似乎與第一個答案分開,只有第一個問題纔有一個突破,任何想法會導致這種情況?以下是產生我在做什麼的代碼:爲什麼mysqli_fetch_array正在分離數組?
// 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` FROM tbl_Answer WHERE QuestionID = 1";
//Run the query
$r = mysqli_query($conn,$q);
//run the answer quert
$r2 = mysqli_query($conn,$q2);
//Assign the AnswerStatusID to a var
$AnswerStatusID[] = $row2['AnswerStatusID'];
while($row = mysqli_fetch_array($r,MYSQLI_ASSOC)){
echo '<div id="Question1"><p>1)' . $row['Question'] . '</p>';
}
while($row2 = mysqli_fetch_array($r2,MYSQLI_ASSOC)){
echo '<input name="q1" type="radio"value="'.$AnswerStatusID.'"/>' . $row2['Answer'] . '</div><br/>';
}
我會首先檢查你的SQL語句。請記住,返回的行可能不會按照您想要的方式排序。很難說出問題和答案是如何構建的,但如果您可以看一下SQL「order_by」,則可以快速解決很多問題。 [MySQL order by](http://dev.mysql.com/doc/refman/5.6/en/order-by-optimization.html) – Stegrex 2012-07-05 15:23:20
您能否包含這實際輸出的內容?我無法從你的描述中完成它。另外,如果只有一個條目,你是否真的需要一個while循環來打印這個問題? – andrewsi 2012-07-05 15:23:27
最終會有多個問題,但是,在繼續之前,我需要這個問題才能正確生成。 @andrewsi我會嘗試截屏併發帖 – 2012-07-05 15:26:29