Grab image from database

Asked

Viewed 1,460 times

-2

wanted to know how I could recover an image from the database, used the php7, the image is recorded in the database in type BLOB, I searched on the internet and were very old examples and none worked here, someone could help me ?

1 answer

0


To insert the image do the following:

$db = mysqli_connect("localhost","root","","DbName"); //keep your db name
$image = addslashes(file_get_contents($_FILES['images']['tmp_name']));
$query = "INSERT INTO products (id,image) VALUES('','$image')";  
$qry = mysqli_query($db, $query);

To access the Blob image

$db = mysqli_connect("localhost","root","","DbName"); //keep your db name
$sql = "SELECT * FROM products WHERE id = $id";
$sth = $db->query($sql);
$result=mysqli_fetch_array($sth);
echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image'] ).'"/>';

I hope it works for you!

  • 1

    Just like that.

  • 1

    I’ll test it here, and bring the feedback to you.

  • 1

    Thank you very much guy gave it right !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.