[英]PHP while loop displays same result over and over again
I'm using a while loop in order to display items from the database but this instead is displaying the same result and ignoring one. 我正在使用while循环以显示数据库中的项目,但这反而显示了相同的结果而忽略了一个。 Example: if the first items is called Item1 and I have 4 items in the database (Item1, Item2, Item3, Item4) it will display 3 times the Item1. 示例:如果第一个项目称为Item1,并且我在数据库中有4个项目(Item1,Item2,Item3,Item4),它将显示3倍于Item1。
<?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";
}
?>
Any ideas how to fix it? 任何想法如何解决?
Try this : 尝试这个 :
$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 />";
}
Explanation: Following code only returns one row. 说明:以下代码仅返回一行。 To get more rows, you need a loop: 要获得更多行,您需要循环:
$card_data_result = mysqli_fetch_array($card_data);
$build_name = $card_data_result['build_name'];
Since you have 4 items, your while
loop will print 4 times $build_name
(the same item) 由于您有4个项目,因此while
循环将打印4次$build_name
(同一项目)
Rewrite your while
like this 像这样重写你的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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.