Jump to content

Retrieving Images from a MySQL database


huntrguy102

Recommended Posts

I have images stored in a mysql database.  I have it as Type: Blob. It is binary data.

 

my first question is: Did I go about putting the image in right?

 

My second question is: How would I go about retrieving these images from the database?

 

a very detailed explanation would be appreciated because I am basically lost at this point.  Thanks.

Link to comment
https://forums.phpfreaks.com/topic/197664-retrieving-images-from-a-mysql-database/
Share on other sites

Did I go about putting the image in right?

 

Yes.

 

How would I go about retrieving these images from the database?

 

<img src="images.php?image_id=<id>" width="<width>" height="<height>">

 

images.php must echo out the contents of the BLOB field

 

ok this is the code for image.php

 

 

<?php

$conn = mysql_connect('localhost,'username','password') or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db('database',$conn) or trigger_error("SQL", E_USER_ERROR);

//table name is books
$sql = "SELECT * FROM books ORDER BY id LIMIT 2";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

//the field with the binary dat is called image
$image = $row['image']; 
header("Content-type: image/jpg"); // or whatever 
print $image; 
exit; 
?>

 

 

is this correct?  when I look at this php in the browser, it gives me a bunch of random symbols and letters and numbers.  Is that supposed to happen?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.