Broken file icon getting image from php and mysql database

I need to download and retrieve an image from a database, I can save the image to the database, but was unable to display it later. please help. I wrote the following code to retrieve from the database.

  $result1=mysql_query("INSERT INTO userdata(id, username, firstname, lastname, imageType, image)VALUES('', '" . $_SESSION['username'] . "', '" . $_SESSION['firstname'] . "', '$lastname','{$image_size['mime']}','{$imgData}')") or die("Invalid query: " . mysql_error());
echo "</br>";
echo "Registration successful";
echo "</br>";
echo $lastid=mysql_insert_id();//get the id of the last record
echo "uploaded image is :"; ?>
<img src="imageView.php?image_id=<?php echo $lastid; ?>" /><br/>

echo "</br>";     
}#if result1into db successful
echo $result1;
echo "Problem in database operation";


imageView.php has the following code:

    $conn = mysql_connect("localhost", "root", "");
    mysql_select_db("wordgraphic") or die(mysql_error());
         if(isset($_GET['id'])) {
        $sql = "SELECT imageType,image FROM userdata WHERE id=". $_GET['image_id'];
        $result = mysql_query("$sql") or die("<b>Error:</b> Problem on Retrieving Image BLOB<br/>" . mysql_error());
        $row = mysql_fetch_array($result);
        header("Content-type: " . $row["imageType"]);
        echo $row["image"];


What could be wrong with the code? When I try to run imageView.php

with a static id, the image is displayed. So I think the error is in passing this code to a variable:

 echo "uploaded image is :"; ?>
<img src="imageView.php?image_id=<?php echo $lastid; ?>" /><br/>


what could be wrong?


source to share

1 answer

just grease fix

<img src="imageView.php?image_id=<?php echo $lastid; ?>" />


instead src = src path where ur saving the image file allows you to scan in the images folder which will $imagedata

save the filename here in the db so you can restore it from the images folder

<img src="images/$imgData" width="your wish" height="your wish"/>




All Articles