2015-11-22 126 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

謝謝它的作品! –