2013-03-16 132 views
0

我寫了一個簡單的代碼來顯示圖像。圖像存儲在一個文件夾中,並且它的名字在mysql中。但問題是隻有第一張圖像顯示的次數與在db中的總圖像次數一樣多。 這裏是我的顯示圖像簡單的代碼:只顯示第一張圖片

$query = "Select * from admin_images"; 
$result = mysql_query($query) or die(mysql_error()); 
$rows = mysql_fetch_array($result) or die(mysql_error()); 
$i=mysql_num_rows($result); 
while($i>=1) 
{ 
    $img = $rows['my_image_name']; 
    echo '<img src="../admin_images/$img">'; 
    $i--; 
} 

圖片名稱保存在admin_images表中my_image_name場與實際圖像存儲在admin_images文件夾中。

回答

1

試圖通過一個循環來獲取圖像的URL。
爲了更好地理解程序流程,請看下面的代碼。

//Query to select the image url from database. 

$query = "Select * from admin_images"; // Try to use proper column name instead of * 
$result = mysql_query($query) or die(mysql_error()); // mysql_* functions are depreciated. So try to avoid using this function. 
$i=mysql_num_rows($result); 

if($i >=1) 
{ 
while($rows=mysql_fetch_array($result)) 
{ 
    $img = $rows['my_image_name']; 
    echo '<img src="../admin_images/$img">'; 

}// The above loop will terminate when the condition becomes false. And hence, till now you will have printed all your images. 
} else { 
echo'No image record found!'; 
} 
2

你必須一前一後取行:

$query = "Select * from admin_images"; 
$result = mysql_query($query) or die(mysql_error()); 
while($row = mysql_fetch_array($result) or die(mysql_error())) 
{ 
    $img = $row['my_image_name']; 
    echo '<img src="../admin_images/$img">'; 
    $i--; 
} 
相關問題