2015-11-22 225 views
-1

我是PHP新手,试图在一个循环中产生像这样的数字,这个循环已经用于从数据库表中获取数据。PHP while while循环不起作用

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

我想停止循环,只要有表中的行。
错误:
它根据行数产生两个,三个图像,但所有图像源1.JPG

+4

您提供的代码不包含您描述的错误。也许包括更多的代码可能会让别人发现问题。 – Tristan

回答

1

抱歉,这并不是一个回答你的问题,但它是唯一的答案可能在此刻:

这个工作对我来说:

<?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 

所以问题不在于你分享的代码。

+0

不能在while循环中放置变量,并且在另一种方法(最后一个)中描述了吗? –

+0

@JahanzaibAsgher这不是一个while循环,它是一个for循环。该代码执行得很好,我测试了它。 – csga5000

+0

谢谢它的作品! –