Skip to content Skip to sidebar Skip to footer

How To Display An Blob Image Stored In Mysql Database?

I am trying to display the last 5 images uploaded to my 'store' table in MySql. I'm a complete noob to PHP and databases and i've been reading a lot on how to do this but no luck.

Solution 1:

This is what I used when I wanted to do something like that... a long time ago! =P

$sql = "SELECT image FROM table WHERE cond ORDER BY xxxx DESC LIMIT 5";
$result = mysqli_query($db,$sql);
while($arraySomething = mysqli_fetch_array($result))
{
    echo"<img src='php/imgView.php?imgId=".$arraySomething."' />";
}

Solution 2:

I try the first approach with header('content-type: image/jpeg'); but end up with image not shown. After a few google through website I found the solution which I can display image from database to my page

try this:

mysql_connect("localhost","root","")ordie("Cannot connect to database"); //keep your db name
mysql_select_db("example_db") ordie("Cannot select database");
$sql = "SELECT * FROM `article` where `id` = 56"; // manipulate id ok $sth = mysql_query($sql);
$result=mysql_fetch_array($sth);
// this is code to display echo'<img src="data:image/jpeg;base64,'.base64_encode( $result['image_file'] ).'"/>'

Solution 3:

mysql_connect("localhost","root","")ordie("Cannot connect to database"); 

//keep your db name
mysql_select_db("example_db") ordie("Cannot select database");

$sql = "SELECT * FROM `article` where `id` = 56"; 
// manipulate id ok $sth = mysql_query($sql);
$result=mysql_fetch_array($sth);
// this is code to display echo'<img src="data:image/jpeg;base64,'.base64_encode( $result['image_file'] ).'"/> width="xxxx" height="xxxx"';

Add the height and width also

Solution 4:

You can also use this function

//Retrieve image from database and display it on html webpage    functiondisplayImageFromDatabase(){    
//use global keyword to declare conn inside a function    global$conn;    
$sqlselectimageFromDb = "SELECT * FROM `imageuploadphpmysqlblob` ";    
$dataFromDb = mysqli_query($conn, $sqlselectimageFromDb);    
while ($row = mysqli_fetch_assoc($dataFromDb)) {    
echo'<img height="250px" width="250px" src=data:image;base64,'.$row['image'].'/>';    
}

Insert it into mysql database like this :

$image = $_FILES['imagefile']['tmp_name'];
$name = $_FILES['imagefile']['name'];
$image = base64_encode(file_get_contents(addslashes($image)));

references : https://mauricemutetingundi.blogspot.com/2019/04/how-to-upload-blob-image-to-mysql.html

Post a Comment for "How To Display An Blob Image Stored In Mysql Database?"