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>

Dani AI

Generated

Good progress — the non-image fields are rendering and the fact you see a “non displayed” image means the blob is present but the browser isn’t getting a valid image resource. As @diafol noted, storing paths and files is generally the better choice for product images (smaller DB, faster backups, browser/CDN caching, easier thumbnails). As said, if you keep blobs you still need a proper <img> src that returns raw image bytes with the correct Content-Type.

Recommended approaches (concise):

  • Preferred: store files (or object storage) and save a path/URL in the DB. Validate uploads, create and store thumbnail variants on upload, and serve images directly via the webserver or a CDN for best performance.
  • If you must keep BLOBs: do not inline large base64 URIs for product images. Instead expose a small image endpoint that streams the binary with proper headers and caching. This keeps your HTML clean and lets the browser cache images.

Minimal example of an image endpoint (use parameterized queries and a real DB connection):

<?php
// image.php?id=123
require 'db.php'; // provide PDO $pdo
$id = (int)($_GET['id'] ?? 0);
$stmt = $pdo->prepare('SELECT product_image, image_mime FROM products WHERE product_id = ? LIMIT 1');
$stmt->execute([$id]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if (! $row) { http_response_code(404); exit; }
$mime = $row['image_mime'] ?: 'image/jpeg';
header('Content-Type: '.$mime);
header('Cache-Control: public, max-age=86400');
echo $row['product_image'];

Notes: store or detect MIME type, keep IDs validated, generate thumbnails to avoid sending full-size images in the table, and use browser DevTools Network/Source views to debug any broken src values.

Recommended Answers

All 5 Replies

Member Avatar for Member #120589

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: ) 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 Member #120589

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.phpro.org/tutorials/Storing-Images-in-MySQL-with-PHP.html

http://phppot.com/php/mysql-blob-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.