Jump to content

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.