BLOB image viewing fetch error!

Sir,
kindly help me as i have been trying to retrieve and view blob type image from database which is also getting saved inside a folder named as upload. this is my code for retrieving data.

?Php
////////////////////////////////////////////
// Collecting data from query string
$id=$_GET['id'];
// Checking data it is a number or not
if(!is_numeric($id)){
echo "Data Error";
exit;
}
// MySQL connection string
require "dbconfig.php";

$count="SELECT * FROM visitors where id=?";

if($stmt = $connection->prepare($count)){
  $stmt->bind_param('i',$id);
  $stmt->execute();
    
 $result = $stmt->get_result();
 
 $row=$result->fetch_object();    
}else{
echo $connection->error;
}
?>

and this is how i retrieve/fetch from database.

<?php echo $row->image ?>

but it throws a long characters and variable… kindly help me.

I found this on a quick search for “display image from blob”:

echo '<img src="data:image/jpeg;base64,'.base64_encode( $row->image ).'"/>';

You need something to tell the browser that you’re starting to send an image to it, or it won’t know how to handle the data.

I have tried this method but I couldn’t view the image. Showing broken image. Kindly help me. Note i have a folder named as upload for saving the image with unique id on my pc and the blob image will get uploaded to database.

This is my action page and how i upload to a folder and save it in database.

<?php
session_start();
//Checking User Logged or Not
if(empty($_SESSION['user'])){
 header('location:/VMS/login.php');
}
$host="127.0.0.1";
$username="root";
$pass="";
$db="VMS";

$conn=mysqli_connect($host,$username,$pass,$db);
if(!$conn){
	die("Database connection error");
}

// insert query for register page
if(isset($_POST['ronel'])){
 // $image = $_FILES['name']['name'];
  //$target_dir = "uploads/";
  //$target_file = $target_dir . basename($_FILES["name"]["name"]);
    
      // Select file type
  //$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

  // Valid file extensions
  //$extensions_arr = array("jpg","jpeg","png","gif","pdf");

  // Check extension
  //if( in_array($imageFileType,$extensions_arr))

$mobile=$_POST['mobile'];
$email=$_POST['email'];
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];    
$purpose=$_POST['purpose'];   
$passtype=$_POST['passtype'];
$company=$_POST['company'];
$validupto=$_POST['validupto']; 
$doctype=$_POST['doctype'];      
$docnumber=$_POST['docnumber'];
$address=$_POST['address'];
$whomtomeet=$_POST['whomtomeet'];     
$designation=$_POST['designation'];
$gender=$_POST['gender'];
$image = $_POST['image']; 
    $imgsrc = "upload/";
  
    $image_parts = explode(";base64,", $image);
    $image_type_aux = explode("image/", $image_parts[0]);
    $image_type = $image_type_aux[1];
  
    $image_base64 = base64_decode($image_parts[1]);
    $fileName = uniqid() . '.jpg';
  
    $file = $imgsrc . $fileName;
    file_put_contents($file, $image_base64);
  
    print_r($fileName);
  
if($_POST['nationality'] === 'others'){
$nationality=$_POST['nationality'];
$country=$_POST['country'];
$passport=$_POST['passport'];
$validity=$_POST['validity'];    

 	 $query="INSERT INTO `visitors` (`mobile`,`email`,`firstname`,`lastname`,`purpose`,`passtype`,`company`,`validupto`,`time`,`date`,`doctype`,`docnumber`,`address`,`whomtomeet`,`designation`,`gender`,`nationality`,`country`,`passport`,`validity`,`image`) VALUES ('$mobile','$email','$firstname','$lastname','$purpose','$passtype','$company','$validupto',current_timestamp(),current_timestamp(),'$doctype','$docnumber','$address','$whomtomeet','$designation','$gender','$nationality','$country','$passport','$validity','$image')";
	
	$res=mysqli_query($conn,$query);
	if($res){
		$_SESSION['success']="Not Inserted successfully!";
		header('Location:');
	}else{
		echo "<script>alert('Data not inserted! Kindly contact ITSUPPORT OTPC');</script>";
	}
      // Upload file
    // move_uploaded_file($_FILES['name']['tmp_name'],$target_dir.$image);
	}
      else {

Why do you store it twice, once in the folder and once in the database?

Sorry, I can’t help any more, this isn’t something I’ve done. I just store images in a folder and store the name in the database, then I can just use a normal <img> tag on them.

ok, sir but I have found a solution to it and what I did was click a snapshot and auto-download it then upload it to the database. Thanks for the help sir.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.