Catch image in database

1

I wanted to know how I could retrieve an image from the database, used php7, the image is stored in the database in type BLOB, I searched the internet and they were very old examples and none worked here, could anyone help me?

    
asked by anonymous 07.03.2017 / 12:48

1 answer

2

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!

    
07.03.2017 / 13:44