Re: Displaying a longblob as an image [message #170248 is a reply to message #170239] |
Fri, 22 October 2010 18:38 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 10/21/2010 5:13 PM, anu wrote:
> I am storing an image as a longblob in MYSQL. I would like to know how
> I can display the image in my form in PHP since the image is stored as
> ascii in the MySQL table.
OK, first of all, one correction: A BLOB is a binary object (hence the
"B" in BLOB), not an ASCII one. And binary data is character-set
independent.
As for displaying the data: Two steps. First you have an img tag in
your html file, as always. However, instead of pointing at the image
itself, it points at a PHP script, passing an identifier to the picture
you want, i.e.
<img src="showimage.php?imageid=42">
Now, assuming your MySQL table ('images') has two columns labeled "id"
and "imagedata" (you could have other columns, also), you could code
showimage.php something like (data validation and error checking left
out to keep them from confusing the matter):
<?php
// Get the id of the image to display
$imageid = $_GET['imageid'];
// Connect to the database - change parms as necessary
$link = mysql_connect('localhost', 'localuser', userpassword');
$db = mysql_select_db('mydatabase');
// Set up the SQL and fetch the data
$sql = "SELECT imagedata FROM images WHERE id=$imageid"):
$result = mysql_query($sql);
$data = mysql_fetch_assoc($result);
// Done with MySQL - clean it up
mysql_free_result($result);
mysql_close($link); // Done with MySQL now
// Your blob is now in $data['imagedata']
$datalen = strlen($data['imagedata']); For the header
// Send the content type
header('Content-Type: image/jpeg'); // Assuming a jpeg
// Content length isn't strictly required, but good to send
header("Content-Length: $datalen");
// And send your image
echo $data['imagedata'];
?>
That's all there is to it
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|