<?php
$con = mysqli_connect("localhost","root","","test") or die('error');
$query = "select * from data" ;
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_row($result)){
$num_id = $row['id'];
$arr_question = $row['question'];
$a = $row['option_a'];
$b = $row['option_b'];
$c = $row['option_c'];
$d = $row['option_d'];
$answere = $row['answere'];
$description = $row['description'];
}
?>
我想使用此代码从数据库中获取数据,但是当我使用 mysqli_fetch_array
它返回表的最后一行,当我使用mysqli_fetch_row
它返回未定义的索引。
现在请帮助我,对我来说应该采取什么正确的步骤?
您在while
循环期间覆盖变量,因此在循环结束时,您的变量将包含最后一行的值。
尝试将每行的值存储到数组中,然后稍后处理数组,您可以使用关联数组,例如:
$results = array();
while($row = mysqli_fetch_row($result)){
$results[] = array(
'num_id'=> $row['id'],
'arr_question' => $row['question'],
'a' => $row['option_a'],
'b' => $row['option_b'],
'c' => $row['option_c'],
'd' = $row['option_d'],
'answer' => $row['answere'],
'description' => $row['description']
);
}
print_r($results);