PHP while循环不是';t工作


PHP while loop isn't working

我是PHP的新手,正在尝试在一个循环中生成这样的数字,这个循环已经用于从数据库表中获取数据。

$i= 1;
while($row = $result1->fetch_assoc()) {
/////////////////other codes
<img src="$i.jpg">
$i++;}

只要表中有行,我就想停止循环
错误:
它根据行数生成两个、三个图像,但所有图像都有源1.jpg

很抱歉,这不是你问题的答案,但这是目前唯一可能的答案:

这对我有效:

<?php
$rows = [
    'item',
    'item',
    'item',
    'item'
];
function fetch() {
    global $rows;
    return count($rows) > 0 ? array_splice($rows,0,1)[0] : null;
    //Should match return behavior of fetch assoc according to: http://php.net/manual/en/mysqli-result.fetch-assoc.php
}
/**///Remove a star to toggle methods
$i = 1;
while($row = fetch()) {
    echo "$i<br>";
    $i++;
}
/*/
//Alternative method:
for ($i = 1; $row = fetch(); $i++)
    echo "Alt: $i<br>";
//*/

输出:

1
2
3
4

所以问题不在于您共享的代码。