Masroor_Shah
Masroor_Shah

Reputation: 313

Broken image retrieved from Mysql in php?

I upload image and store its path or directory to database that is successfully stored but now when i retrieved image from database broken image load here plz help me out thanks in advance. I already checked these stackoverflow questions 1 2 and 3

My display script

<?php
include("configdb.php");

$select_query = "SELECT 'images_path' FROM  `images_tbl` ORDER by 'images_id' DESC";
$sql = mysqli_query($conn,$select_query) or die(mysqli_error($conn));	
while($row = mysqli_fetch_array($sql,MYSQL_BOTH)){
   
}
?>

and uploading script

<?php
include("configdb.php");
    function GetImageExtension($imagetype)
     {
       if(empty($imagetype)) return false;
       switch($imagetype)
       {
           case 'image/bmp': return '.bmp';
           case 'image/gif': return '.gif';
		   case 'image/jpeg': return '.jpg';
           case 'image/png': return '.png';
           default: return false;
       }
     }
if (!empty($_FILES["uploadedimage"]["name"])) {
    $file_name=$_FILES["uploadedimage"]["name"];
    $temp_name=$_FILES["uploadedimage"]["tmp_name"];
    $imgtype=$_FILES["uploadedimage"]["type"];
    $ext= GetImageExtension($imgtype);
    $imagename=date("d-m-Y")."-".time().$ext;
    $target_path = "../Photos/".$imagename;
if(move_uploaded_file($temp_name, $target_path)) {
    $query_upload="INSERT into images_tbl (`images_path`,`submission_date`) VALUES
('".$target_path."','".date("Y-m-d")."')";    
mysqli_query($conn,$query_upload) or die("error in $query_upload == ----> ".mysqli_error($conn)); 
}else{
   exit("Error While uploading image on the server");
}
}

echo "<img src='displayupload.php?id=7' width='200 px' height='200px' />";

?>;

Upvotes: 2

Views: 257

Answers (1)

Rebecca Joanna
Rebecca Joanna

Reputation: 335

try like this

<?php
include("configdb.php");

$select_query = "SELECT 'images_path' FROM  `images_tbl` ORDER by 'images_id' DESC";
$sql = mysqli_query($conn,$select_query) or die(mysqli_error($conn));	
while($row = mysqli_fetch_array($sql,MYSQL_BOTH)){
	
	
	
	
	
$row = mysqli_fetch_array($conn,$sql);
        header("Content-type: " . $row["images_id"]);
        echo $row["images_path"];
         }
    mysqli_close($conn);



?>

and display it from the path where you have saved or stored that like

<?php
include("configdb.php");
    function GetImageExtension($imagetype)
     {
       if(empty($imagetype)) return false;
       switch($imagetype)
       {
           case 'image/bmp': return '.bmp';
           case 'image/gif': return '.gif';
		   case 'image/jpeg': return '.jpg';
           case 'image/png': return '.png';
           default: return false;
       }
     }
if (!empty($_FILES["uploadedimage"]["name"])) {
    $file_name=$_FILES["uploadedimage"]["name"];
    $temp_name=$_FILES["uploadedimage"]["tmp_name"];
    $imgtype=$_FILES["uploadedimage"]["type"];
    $ext= GetImageExtension($imgtype);
    $imagename=date("d-m-Y")."-".time().$ext;
    $target_path = "../Photos/".$imagename;
if(move_uploaded_file($temp_name, $target_path)) {
    $query_upload="INSERT into images_tbl (`images_path`,`submission_date`) VALUES
('".$target_path."','".date("Y-m-d")."')";    
mysqli_query($conn,$query_upload) or die("error in $query_upload == ----> ".mysqli_error($conn)); 
}else{
   exit("Error While uploading image on the server");
}
}

echo "<img src='../Photos/$imagename' width='200 px' height='200px' />";

?>;

Upvotes: 2

Related Questions