Displaying multiple images, BLOB data, from a MySQL database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
back_up
Forum Newbie
Posts: 2
Joined: Sun Dec 13, 2009 11:18 am

Displaying multiple images, BLOB data, from a MySQL database

Post by back_up »

Need to display multiple images stored as BLOB data types in a MySQL table. Using a while loop, other fields in the same table with text data type, displays in a browser showing all rows when the BLOB field is not included.

The BLOB data (the images), often are displayed as binary data but needs to show as actual images.

Apparently there is an issue with PHP with displaying the binary data stored in a variable directly as an image when echoing or printing the variable with BLOB data using echo $TheBlobData; or print $TheBlobData;.

In some instances, the query executes, retrieves all rows but only displays an image, instead of all the images per row.

Storing the images in directories has security issues, other risk and the BLOB data needs to be stored and pulled directly from the database.


--------------------
Field Name, DataType
---------------------
ID, int
Picture, BLOB
Type, VARCHAR

For example this prints the ID of each row:
while ($rows = mysql_fetch_object($result))
{
echo $rows->ID;
}
and this prints the image Type stored in a field of each row

while ($rows = mysql_fetch_object($result))
{
echo $rows->Type;
}

but this prints each row of the images as binary data but needs to print actual images:

while ($rows = mysql_fetch_object($result))
{
echo $rows->Picture;
}

When inserting the header() function to describe the image data type, it only prints an image using the same while loop, in some cases it simply shows a blank page and echos the address of the page:

while ($rows = mysql_fetch_object($result))
{
header ();
echo $rows->Picture;
}

This requires printing the multiple BLOB data directly as images to a page for each row in a table and not just an image on the page. For users of Postgres SQL, is this also an issue and are they open source databases where BLOB data types can be displayed directly as images by echoing or printing the BLOB data variable?

Should you intend to respond, read carefully and ensure postings are pertinent.
Last edited by back_up on Mon Dec 14, 2009 10:10 am, edited 2 times in total.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Displaying multiple images, BLOB data, from a MySQL database

Post by AbraCadaver »

Browsers don't understand the blob data unless you send the proper headers. You can only do this once per page. What you need is a loop that echos an img tag with the src set to a php file that will grab one image data and echo it with proper headers:

Code: Select all

// your php script
while ($row = mysql_fetch_object($result)) {
    echo '<img src="show_image.php?id=' . $row->ID . '">';
}

Code: Select all

// show_image.php
$result = mysql_query('SELECT `Picture`, `Type` FROM `db` WHERE `ID` = ' . (int)$_GET['id']);
$row = mysql_fetch_object($result);
header('Content-type: image/' . $row->Type);
echo $row->Picture;
Also, mysql_fetch_row() doesn't return an object.
Last edited by AbraCadaver on Fri Dec 18, 2009 8:00 pm, edited 1 time in total.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Displaying multiple images, BLOB data, from a MySQL database

Post by AbraCadaver »

mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply