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";
}