Display image from sql

2019-03-01 15:29发布

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);

                ?>

标签: php sql blob
5条回答
虎瘦雄心在
2楼-- · 2019-03-01 15:46

The debate of storing blobs versus storing a path to the image file on disk has been debated over and over again. Microsoft provides a research paper comparing the pros and cons of each here. With that said, to display a blob as an image you need to make a call to a separate page and output header information that tells the browser what type of image is stored.

For example:

connectToDatabase();

$sql = "SELECT image_blob FROM eportal;";

$result = mysql_query($sql) or die(mysql_error());  
$row = mysql_fetch_array($result);

header("Content-type: image/jpeg");
echo $row['image_blob'];
$db->close();
查看更多
对你真心纯属浪费
3楼-- · 2019-03-01 15:54

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

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

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 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.

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

查看更多
姐就是有狂的资本
4楼-- · 2019-03-01 16:07

Read the Blob data and write it into the file with header type image.. and try to print it, It should display the image file.

And yes saving image or any file in DB is really a bad habit as you are increasing DB size and it slowdown the performance also.. I suggest you to just try to convert you Blob into Image but don't apply in your work. Just save the image at desired location and keep its location path into DB to fetch and save next time.

查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-03-01 16:08

Saving images to the DB is not a good idea but if You think You need to it this way, then You can retrieve the data from DB table, encode it to base64 (http://php.net/base64_encode) and then in HTML print it in this way:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">

Using PHP You would write:

echo '<img src="data:'.$image_mime_type.';base64,'.base64_encode($image_data_from_db).'" alt="My image alt" />';
查看更多
闹够了就滚
6楼-- · 2019-03-01 16:13

In case you still want to save your images in database, you will need second script which will get those images from database and pass them to browser with correct headers.

header("Content-type: image/jpeg");
# 
# Replace this with database read:
# readfile('myimage.jpg');

Also, you will need to store what kind of image u use. There will be different header for JPEG, GIF or PNG file.

查看更多
登录 后发表回答