0

I'm having a very odd problem with PHP/MySQL...

echos "id;id"

 foreach($ids as $id) {
      echo "id;";
 }

echos "id;id;id;id;..." (infinite loop)

 foreach($ids as $id) {
      while($row = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM `Table` WHERE `id`=$id;") {
           echo "id;";
      }
 }

The reason I have a foreach() statement is because I have $ids sorted.

4

1 回答 1

1

获得一个无限循环,这仅仅是因为您每次通过while循环都重新运行查询。这意味着,对于每个$id,您不断运行查询并从中提取第一行。因此,对于返回多于零行的任何查询,该查询的持续重新执行将使while循环无限循环。

它在功能上类似于下面的伪代码,它也将永远循环,因为您正在修改循环中的控制条件:

loop i from 1 to 10
    set i to 1
endloop

您应该尝试以下方法:

foreach($ids as $id) {
    $result = mysqli_query($conn, "SELECT * FROM `Table` WHERE `id`=$id;");
    while($row = mysqli_fetch_array($result)) {
       echo "id;";
    }
}

这将为每个 ID运行一次while查询,循环将处理该查询的行。

于 2013-04-24T01:10:03.903 回答