从SQL显示图像 [英] Display image from sql

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

问题描述

我创建了一个表,其中已通过"BLOB"保存了图像.我需要将这些图像与其他项目一起显示.但是我不知道如何在同一页面中一起显示这些图像. 这是我的php代码,以表格形式显示其他内容.同样,我想相应地显示图像.有帮助吗?

I've create a table where I've saved images through "BLOB". I need to show those images along with other items. But I don't know how to show those images together in the same page. Here's my php code that displays other things in form of a table. Similarily, I wanted to display images accordingly. Any help?

<?php
                $display_query = mysql_query("SELECT * FROM eportal");

                    echo "<table id='pageTable'><thead><tr><th>Item code</th><th>Description</th><th>Cost</th></tr></thead>";
                    echo "<tbody>";
                    while($row = mysql_fetch_array($display_query)){
                        print "<tr><td>".$row['itemid']."</td><td>".$row['description']."</td><td>";
                        print "&#8377;".$row['cost']."</td></tr>";
                    }
                    echo "</tbody>";
                    echo "</table>";
                    mysql_close($connection);

                ?>

推荐答案

就像其他人提到的那样,将图像存储在数据库中通常是个坏主意.

As other people mentioned, storing the images in the database is usually a bad idea.

图像不能与另一个页面数据以相同的HTTP响应传输.

Images are not transmitted in the same HTTP response with another page data.

要显示数据库中的图像,您需要实现一个脚本,给定项目ID,该脚本将读取字段并发送图像的二进制数据.并在表单的<img src="">中提供脚本的路径:

To show images from the database, you would need to implement a script which, given the item id, would read the field and send the image's binary data; and provide the path to the script in your form's <img src="">:

while($row = mysql_fetch_array($display_query)){
    print "<tr><td>".$row['itemid']."</td><td>".$row['description']."</td><td>";
    print "&#8377;".$row['cost']."</td><td>";
    print "<img src=\"image.php?id=".$row['id']."\"></td></tr>";

}

image.php是另一个脚本,该脚本在给定eportal.id的情况下输出image_blob的值.您还需要在标题中提供正确的大小和mime类型.

image.php is another script which outputs the value of image_blob given the eportal.id. You would also need to provide correct size and mime type in the headers.

最好将图像存储在文件中(可通过Web服务器访问),然后将文件的路径存储在数据库中.

You better just store the images in a file (accessible by the web server) and store the path fo the file in the database.

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

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