且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用PHP显示多个图像(斑点)从MySQL?

更新时间:2023-02-04 15:15:13

解决此问题的一种可能方法问题是要有一个单独的脚本来动态输出图像的内容,例如。 :

image.php

 标题('Content-type:image / jpg'); 

// DataBase在这里查询和处理...

echo $ data ['myImage'];

并且在需​​要显示存储在数据库中的图像时调用它,例如。在你的循环中:

  echo'< img src =image.php?id ='。$ data ['id' ]。'>'; 

但将数据存储在数据库中 会对服务器造成影响 ,除非它们非常小或者您有一个很好的理由才能这样做,您应该只将它们的物理位置存储在磁盘上。



如果您希望隐藏用户的图像位置或控制访问权限,也可以使用此方法,但对于该情况,有更好更快的选择。


I'm trying to display multiple images using PHP and MySql database, even if using the while loop I don't get all of the images, I only get one, I mean the first one in the table. What's the problem ?

I'm using a table ID_IMAGE (int, pk, auto increment) and myImage (blob)

$query = mysql_query("SELECT myImage FROM image");
while($data=mysql_fetch_array($query)) {
    header('Content-type: image/jpg');
    echo $data['myImage'];
}

A possible way to solve this problem is to have a separate script to dynamically output the contents of the image eg. :

image.php

header('Content-type: image/jpg');

// DataBase query and processing here...

echo $data['myImage'];

and call it whenever you need to show images stored in your DB eg. inside your loop:

echo '<img src="image.php?id=' . $data['id'] . '">';

But storing images in the database will take a toll on your server and unless they're really small or you have a good reason to do so, you should only store their physical location on the disk.

You can also use this approach if you wish to hide image location from your users, or control access, but there are better and faster alternatives for that case.