2

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

                ?>
xan
  • 4,640
  • 13
  • 50
  • 83
  • 3
    Saving Image into database is bad idea, you should store only path of image in database, and store image on the disk, please check my [answer ](http://stackoverflow.com/a/9141116/996493) – Lucifer Jun 01 '12 at 12:52
  • I agree with Lucifer. It also means that your images, etc, can be stored on a server other than your SQL Server. This frees up capacity on your SQL Server (network, cpu, disk, everything) for actual SQL work, rather than acting as a file server. – MatBailie Jun 01 '12 at 12:54
  • Okay. So I should just save it on my disk and save the path on the database and using "echo"/"print"; display the images one by one as it loops. – xan Jun 01 '12 at 12:58

5 Answers5

2

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" />';
shadyyx
  • 15,825
  • 6
  • 60
  • 95
  • This worked for me: `echo '';` . Actually, I was a newbie. Knowing the basic concepts helped me tackle it. – xan Jun 22 '12 at 15:19
  • Then it means that You have only the filename without extension saved in DB. Think about storing of extension in another column as users could upload also a JPEG or GIF images and then Your script won't work... Now Your solution is totally different to original question... – shadyyx Jun 22 '12 at 15:33
  • No, I haven't saved any "image" column. I just ran the **while** loop and named my images accordingly. Did you get it? Or should I link my entire code? Isn't this a decent-enough approach to a small site? – xan Jun 22 '12 at 15:39
  • You wrote "I've create a table where I've saved images through BLOB" - so I think I had answered accordingly... Now the solution is totaly different from the original problem...or maybe I just don't get it...let it be. – shadyyx Jun 22 '12 at 20:25
  • Work like a charm for images saved as BLOB. – Theotonio Sep 02 '20 at 14:58
0

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.

manurajhada
  • 5,284
  • 3
  • 24
  • 43
0

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.

Quassnoi
  • 413,100
  • 91
  • 616
  • 614
  • I did it by writing : `echo '';` . Actually, I was a newbie. But as the concepts became clear to me about PHP; I tackled it. Thanks. – xan Jun 22 '12 at 15:18
0

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.

Franto
  • 16
  • 3
  • This worked for me: `echo '';` . Actually, I was a newbie. Knowing the basic concepts helped me tackle it. – xan Jun 22 '12 at 15:19
0

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();
Sean
  • 747
  • 4
  • 8