我需要显示MySQL数据库中的图像列表并显示图像。这里我的示例代码适用于插入,但它不显示任何内容..任何人都可以帮助我..
$file = fopen("switch.jpg", "rb");
$image = fread($file, filesize('switch.jpg'));
$image = base64_encode($img);
$ins_query="INSERT INTO mytable (id,imag) "."VALUES ('','$img')";
mysql_query($ins_query)or die('Error in query !');
$id1=1;
echo "inserted ";
$query="select imag from mytable where id='$id1'";
$result=mysql_query($query) or die("Error: ".mysql_error());
$row=mysql_fetch_array($result);
echo '<img src="data:image/jpeg;base64',base64_encode($row['imag']).'"/>';
fclose($file);
答案 0 :(得分:0)
你做错了两件事:
,
:image / jpeg; base64 所以,这是我的修复,试试这个:
...
echo '<img src="data:image/jpeg;base64,',base64_decode($row['imag']).'"/>';
fclose($file);
答案 1 :(得分:-2)
尝试将图像路径或文件名存储在数据库中,而不是在数据库中保存二进制数据。 将图像存储在系统文件夹中。这可能会更快
要获取图片,只需获取图片路径或名称并在html页面中显示
echo '<img src="<?php echo FULL_BASE_URL.'/'.$imagePathfromDB; ?>"/>';
如果ID自动递增,请不要插入。它会自动插入
答案 2 :(得分:-2)
<?php
$number_of_thumbs_in_row = 4;
$result = mysql_query( "SELECT photo_id,photo_caption,photo_filename,photo_category FROM gallery_photos");
while( $row = mysql_fetch_array( $result ) )
{
$result_array[] = "<img src='".$images_dir."/tb_".$row[2]."' border='0' alt='".$row[1]."'/><br>$row[1]<br>$row[3]<br>$row[0]";
}
mysql_free_result( $result );
$result_final = "<tr valign='top' align='center' class='style1'>\n";
foreach($result_array as $thumbnail_link)
{
if($counter == $number_of_thumbs_in_row)
{
$counter = 1;
$result_final .= "\n</tr align='center' class='style1'>\n<tr align='center' class='style1'>\n";
}
else
$counter++;
$result_final .= "\n<td class='style1'>".$thumbnail_link."</td>\n";
}
if($counter)
{
if($number_of_photos_in_row==$counter)
$result_final .= "\n<td class='style1' colspan='".($number_of_photos_in_row=$counter)."'></td>\n";
$result_final .= "</tr>";
}
}
echo <<<__HTML_END
<html>
<head>
<title>Gallery View</title>
</head>
<body>
<table width='100%' border='0' cellpadding="10" cellspacing="10">
$result_final
</table>
</body>
</html>
__HTML_END;
?>
只需浏览this article。