2017-06-22 60 views
0

我要echoimg每次$i增加,但它只有echo s最後$i如何在php中運行一個mysql查詢servel次數

for($i=0; $i<=5;$i++) 
    $sql="SELECT DISTINCT id, Make FROM sve WHERE id='".$i."'"; 

    $rezult=mysqli_query($db, $sql); 
    $red=mysqli_fetch_object($rezult); 
    echo "<img src='brend/brend$red->id.png' class='imgbrend' strana='".$red->Make."'/>"; 

回答

2

您可以使用MySQL的INBETWEEN關鍵字,而不是使用一個for循環,然後遍歷結果集。

您的查詢就會變成這樣的事:

SELECT DISTINCT id, Make FROM sve WHERE id BETWEEN 0 AND 5 

然後,你需要一個循環,像這樣:

while ($red = mysqli_fetch_object($rezult)) { 
    echo "<img src='brend/brend" . $red->id . ".png' class='imgbrend' strana='" . $red->Make . "'/>"; 
} 

全碼:

$sql = "SELECT DISTINCT id, Make FROM sve WHERE id BETWEEN 0 AND 5"; 

$rezult = mysqli_query($db, $sql); 
while ($red = mysqli_fetch_object($rezult)) { 
    echo "<img src='brend/brend" . $red->id . ".png' class='imgbrend' strana='" . $red->Make . "'/>"; 
} 
+1

感謝的人我忘了泰德MySQL有BETWEEN –