對于下面的代碼,$result 將從我的 question_answers 表中回傳 4 行,這些行具有相應的 question_id。
目前我正在將所有 4 行提取到 $answers 變數中。我想知道如何將每一行與結果分開并將它們存盤為單獨的變數,以便我可以一次列印一個。
謝謝。
編輯:此外,由于 question_id 列是 question_answers 表和 $question["question_id"] 中的整數,我是否正確撰寫了我的 WHERE 陳述句?謝謝。
$result = mysqli_query($connection, 'SELECT FROM questions_answers WHERE question_id='".$question["question_id"]."' ORDER BY RAND()'); //Runs the select query to get the possible answers to the randomly selected question
if(mysqli_num_rows($result)>0){
$answers = mysqli_fetch_assoc($result);
//printf each row individually
}
else{
printf("ERROR: no answers for this question exist!");
}
uj5u.com熱心網友回復:
你可以像下面這樣做:
$i=1;
while(${"row" . $i} = mysqli_fetch_assoc($result)){$i ;};
因此,如果有 4 個結果,您將獲得 $row1、$row2、$row3 和 $row4 中的行;
但更標準的方法是將行保存在陣列中,然后使用該陣列:
while($rows[] = mysqli_fetch_assoc($result));
在這種情況下,您可以訪問 $rows['0']、$rows['1']、$rows['2'] 和 $rows['3'] 等行
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/322922.html
上一篇:基于公共元素合并2個陣列
