How to display an BLOB image stored in MySql database?

孤街醉人 提交于 2019-11-27 05:12:02

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."' />";
}
jerza

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","")or die("Cannot connect to database"); //keep your db name
mysql_select_db("example_db") or die("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'] ).'"/>'
mysql_connect("localhost","root","")or die("Cannot connect to database"); 

//keep your db name
mysql_select_db("example_db") or die("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

You can also use this function

//Retrieve image from database and display it on html webpage    
function displayImageFromDatabase(){    
//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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!