broken file icon retrieving image from database php and mysql

倾然丶 夕夏残阳落幕 提交于 2020-01-24 12:05:36

问题


I need to upload and retrieve image from a database, I am able to store the image in the database but 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());
if($result1)
{
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/>

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

imageView.php has the following code:

    <?php
    $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"];
         }
    mysql_close($conn);
?>

What could be possibly wrong with the code? When i try to run imageView.php with static id image is getting displayed. So i guess the error is in passing the variable is this code:

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

what could be possibly wrong?


回答1:


just a smal rectification

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

instead of this src = src path where u r saving the image file lets say in images folder that would be here $imagedata store the file name in the DB so that you can retreive it from the image folder

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


来源:https://stackoverflow.com/questions/29823084/broken-file-icon-retrieving-image-from-database-php-and-mysql

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