我正在使用while循环来显示数据库中的项目,但这显示的是相同的结果而忽略了一个。 示例:如果第一项称为Item1且数据库中有4项(Item1,Item2,Item3,Item4),则它将显示Item1的3倍。
<?php
$card_data = mysqli_query($con, "SELECT * FROM builds");
$card_data_result = mysqli_fetch_array($card_data);
$build_name = $card_data_result['build_name'];
while ($card_data_result = mysqli_fetch_array($card_data)){
echo "$build_name";
}
?>
任何想法如何解决?
答案 0 :(得分:3)
试试这个:
$card_data = mysqli_query($con, "SELECT * FROM builds");
//$card_data_result = mysqli_fetch_array($card_data);
//$build_name = $card_data_result['build_name'];
while ($card_data_result = mysqli_fetch_array($card_data)){
echo $card_data_result['build_name']."<br />";
}
说明: 以下代码仅返回一行。要获得更多行,您需要一个循环:
$card_data_result = mysqli_fetch_array($card_data);
$build_name = $card_data_result['build_name'];
由于您有4个项目,因此while
循环将打印4次$build_name
(同一项目)
答案 1 :(得分:1)
重写您的 while
while ($card_data_result = mysqli_fetch_array($card_data)){
echo $card_data_result['build_name']; //<---- Brought this statment inside what you had outside of the while loop
}