[英]Separate rows from a multi-row result in MySQL PHP
對於下面的代碼,$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!");
}
你可以像下面這樣做:
$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'] 等行
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.