如何使用PHP显示多个图像(斑点)从MySQL? [英] how to Display Multiple Images (blob) from mysql using php?

查看:113
本文介绍了如何使用PHP显示多个图像(斑点)从MySQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用PHP和MySql数据库显示多个图像,即使使用循环我没有得到所有的图像,我只得到一个,我的意思是表中的第一个。问题是什么?



我使用的表 ID_IMAGE(int,pk,自动增量) myImage(blob)

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


解决方案

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

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.

这篇关于如何使用PHP显示多个图像(斑点)从MySQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆