在PHP中显示数据库中的图像 [英] Display image from database in PHP

查看:81
本文介绍了在PHP中显示数据库中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用PHP从数据库中读取图像,但是它在整个页面上显示图像。我想在html页面上显示图像。请帮助

i was trying to read an image from database in PHP,but it was showing image on whole page. i want to display image on a html page. please help

 $sql = "SELECT * FROM images";
    $result = mysql_query($sql) or die("Invalid query: " . mysql_error());

    while($row = mysql_fetch_array($result))
          { 
             header("Content-type: image/jpeg");            
              echo $row[1];                     
          }     


推荐答案

好,如果存储了图像在db中作为blob类型...这是个主意(我想还有很多更优雅的解决方案,但是这一方法也可以使用)。

Ok, if image is stored in db as blob type... here is idea (i guess there are more elegant solutions, but this one is working too).

在主文件中,html内输出,进行以下循环(当然,session_start()在页面开头):

In your main file, inside html output, make this loop (session_start() at the begining of page, of course):

$i=-1;

    while($row = mysql_fetch_array($result))
          { 
          $i++;



         $_SESSION['img'][]=$row['data']; //'data' is field name in my db, you //should change it to appropriate field name... 



         echo "<img src='img.php?id=".$i."'>";





          } 

您的img.php应该看起来像这样:

Your img.php should look like this:

session_start();
header("Content-type: image/jpeg");
$id=$_GET['id'];
echo $_SESSION['img'][$id];
exit;

因此,这将适用于图像数组,也适用于单个图像...- >代码将图像数据存储在会话数组中,img.php显示数组中的图像,在主页的内部循环中……

So, this will work with array of images, and with single image, too... So -> code stores image data in session array, img.php showing images from array, inside loop on main page...

这篇关于在PHP中显示数据库中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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