0

I am using PHP and Mysql to store image in MySQL Database. The image is getting stored in Blob field called B_Image. However, when i try to retrieve the image, it shows characters instead of the image.

I know i still need to write some code to validate certain details. But first i wanted to try and see if it would upload and retrieve the image.

Could you please help me in displaying the image.

My UploadImage.php code:

include"config.php"; //code to connect to my database $file =$_FILES['txtBookCover']['tmp_name']; $Title = $_POST[txtBookName]; $Author = $_POST[txtBookAuthor]; $ISBN = $_POST[txtISBN]; $Type = $_POST[lstGenre]; $image = addslashes(file_get_contents ($_FILES['txtBookCover']['tmp_name'])); echo $image_name = addslashes($_FILES['txtBookCover']['name']); $image_size = getimagesize($_FILES['txtBookCover']['tmp_name']); $SqlStatement= "INSERT INTO Books(B_Title, B_Author, B_ISBN, B_Image, B_ImageName) VALUES('$Title', '$Author', '$ISBN','$image','$image_name')"; if (mysql_query($SqlStatement)) { print "Image has been uploaded!"; } else { print "Error uploading image"; } 

My DisplayImg.php code:

 include"config.php"; $SqlStatement="select B_Image,B_Author,B_ID FROM Books where B_Author='ff'"; $result=mysql_query($SqlStatement); if (mysql_query($SqlStatement)) { echo"hellooo"; while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "Author : {$row['B_Author']} <br>" . "ID : {$row['B_ID']} <br><br>"; echo "<br>"; header('Content-type: image/jpg'); echo "Image: {$row['B_Image']}<br>"; } } else { echo "error"; } 
1
  • 1
    Don't images go in img tags? Commented May 30, 2015 at 9:08

1 Answer 1

1

I think it is a bad idea storing images in the database. Usually what we only store on the database is the path of the image, and the image itself is stored on the server. Good Luck. :)

Sign up to request clarification or add additional context in comments.

6 Comments

In this instance, it's probably fine; a good idea, even!. The images are likely to be very small, and certainly well under the 250k at which point this idea appears to become inefficient.
Oh, I see, But yeah you're right. If we're talking about millions or billions of images here, then it is not very efficient to store it in the database. Thanks for pointing that out @Strawberry.
No. The quantity of images is probably fairly irrelevant. It's the size of them that counts, but as long as they are under 20k, say, then it should be fine.
Thanks everyone! I will try to store them on server and save the path in the database!
I tried to save the photos under the folder 'photos' on the server. It says "Image has been uploaded", however the folder is empty. Could you please help me:
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.