我写了一个简单的代码来显示图像。图像存储在一个文件夹中,其名称存储在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--;
}
图像名称保存在my_image_name字段的admin_images表中,实际图像存储在admin_images文件夹中。
答案 0 :(得分: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--;
}
答案 1 :(得分:1)
尝试通过循环获取图像网址 为了更好地理解程序流程,请查看下面的代码。
//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!';
}