I have a question about inserting images into a product page. I have 5 images (saved as longblobs in my products table) and would like to put them in my table like the id, name, quantity and price.... any idea of how i would do this?

I have this so far. When i run page the id, title, price and availability all work as does the add to cart href part. its just the image i am stuck on

<tr>
                    <td><?php echo $row['product_id'] ?></td>
                    <td><?php echo $row['product_title'] ?></td>
                    <td><?php echo $row['product_price'] ?></td>
                    <td><?php echo $row['product_availability'] ?></td>
                    <td><?php echo $row['product_image'] height="150" width="150" ?></td>
                    <td> </td>
               <td><a href="index2.php?page=productDetails&action=add&id=<?php echo $row['product_id']?>">ADD TO CART</a></td>
</tr>

Recommended Answers

All 5 Replies

Member Avatar for diafol

Do you really need to store them as blobs? Can't you just store the path?

Storing images as blobs will severely impact on DB performance. Also because you have to render the image from code using base64 encoding, you cannot cache the image. Double doh!

So before I offer a solution (although a simple search will show you how e.g. 'get image from blob data php mysql') - what do you think about that?

My database doesnt have a path option and it only seems to display when i save them as blobs. they are quite large images. its not so much for performance but more aesthetic reasoning

tried looking for a solution and havent found an answer yet so any help would be welcome

heh... I think diafol was trying to say why not store the images on a web server, and only store the paths (ex: http://mysite.com/images/myimage.jpg) to the images on that web server in the database - that way you are only retrieving a small amount of text (the URL) as opposed to base64 encoded garbage, that will take however long the transfer time is to load EVERY SINGLE TIME you load a page - as opposed to a cached image (png, jpg, etc...) that will only have to load once, and then any time the user comes back the image is ready to go and will significantly lower your page load times.

While storing image data in the database is not a bad thing, it certainly is a "right tool for the job" sort of thing.

If you are going the database route - which you seem intent on doing - then you will still need a valid html <img> tag, and set it's source to the output of the database.

While databases are very fast, in terms of web development, in general, the database is the slowest component. Bogging it down by doing a very long read with upwards to a megabyte of data for each image, will only make things slower.

Member Avatar for diafol

That's a strange explanation Mark. I don't think I understand 'path option'. And if they are large images, they definitely shouldn't be stored in the DB (IMO).

Is this DB yours? Are you free to change the structure? If so, theres nothing stopping you from storing the filename of the image and storing the actual image anywhere you like. The directory path could be hard-coded into your PHP, that way, if you decide to move the location of your files or rename the image directory, you do not have to change every value in your DB table.

An example:

IMAGES TABLE

id | title | description | width | height | filesize | filetype | filename ...

Say you get a request for image 34:

SELECT ... FROM images WHERE id = 34 LIMIT 1

obviously you'd have a placeholder like ? instead of a literal 34

So get the data into a $row variable or variable name of your choice.

You can spit out the image thus:

$directory = '/upload_images/'; 
$file = $directory . $row['filename'];

Where you need it:

<img src = "<?=$file ?>" />

If you're hell-bent on using blobs:

http://stackoverflow.com/questions/20556773/php-display-image-blob-from-mysql

http://stackoverflow.com/questions/13225726/i-need-my-php-page-to-show-my-blob-image-from-mysql-database

http://stackoverflow.com/questions/13214602/how-to-display-an-blob-image-stored-in-mysql-database

http://www.mysqltutorial.org/php-mysql-blob/

https://www.youtube.com/watch?v=kPGxWaIhLmk

http://www.anyexample.com/programming/php/php_mysql_example__image_gallery_%28blob_storage%29.xml

http://www.phpro.org/tutorials/Storing-Images-in-MySQL-with-PHP.html

http://phppot.com/php/mysql-blob-using-php/

http://forum.codecall.net/topic/40849-tutorial-storing-images-in-mysql-with-php-part-ii-display-your-images/

http://www.codeproject.com/Questions/648955/How-to-display-Images-blob-from-mysql-using-php

Sorry, got bored. Hmm - hard to find?

i have updated my image line to this. It comes up as a non displayed image but it a step forward from what I had

<td> <img src="data:image/jpeg;base64,'.base64_encode($row['product_image']).'" height="150" width="150"/> </td>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.