如何从数据库中显示图像

时间:2014-05-07 13:06:48

标签: php mysql

我尝试从数据库中检索图像和内容但是内容正在显示但是图片没有显示请任何人帮我怎么做。我能够插入图像和内容。你可以看到我的网站http://prawahindia.com/development/blogdisplay.php

   //this is display code
 <?php
    include('connects.php');
    $select_posts = "select * from testblob";
     $run_posts = mysql_query($select_posts); 
    while($row=mysql_fetch_array($run_posts))
   {
    echo '<p class="games_image_result"><img src="'.$row->image.'" width="175"  height="200" />';
      echo '<p class="games_content_result">' .$_content = $row['body'];

     }
   ?>
       <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

   <html>
   <head><title>File Upload To Database</title></head>
  <body>
  <h2>Please Write a comment</h2>
   <form enctype="multipart/form-data" action="<?php echo    htmlentities($_SERVER['PHP_SELF']);?>" method="post">
   <input type="hidden" name="MAX_FILE_SIZE" value="99999999" />
   <div><input name="userfile" type="file" /></div>
    <div><textarea name="body"  style="width:200px; height:100px;"></textarea></div>
   <div><input type="submit" value="Submit" /></div>
    </form>

  </body></html>
 <?php

 if(!isset($_FILES['userfile']))
    {
     echo '<p>Please select a file</p>';
   }
  else
    {
     try    {
    upload();

    echo '<p>Thank you for submitting</p>';
    }
   catch(Exception $e)
    {
    echo '<h4>'.$e->getMessage().'</h4>';
    }
   }
  ?>

   <?php

   function upload(){

       if(is_uploaded_file($_FILES['userfile']['tmp_name']) && getimagesize($_FILES['userfile']['tmp_name']) != false)
    {

   $size = getimagesize($_FILES['userfile']['tmp_name']);

$type = $size['mime'];
$imgfp = fopen($_FILES['userfile']['tmp_name'], 'rb');
$size = $size[3];
$name = $_FILES['userfile']['name'];
$maxsize = 99999999;
$body=$_POST['body'];

if($_FILES['userfile']['size'] < $maxsize )
    {

    $dbh = new PDO("mysql:host=localhost;dbname=abc", 'root', 'root');

            $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);            
    $stmt = $dbh->prepare("INSERT INTO testblob (image_type ,image, image_size, image_name,body) VALUES (? ,?, ?, ?, ?)");


    $stmt->bindParam(1, $type);
    $stmt->bindParam(2, $imgfp, PDO::PARAM_LOB);
    $stmt->bindParam(3, $size);
    $stmt->bindParam(4, $name);
    $stmt->bindParam(5, $body);


    $stmt->execute();
    }
else
    {

    throw new Exception("File Size Error");
    }
   }
else
  {

  throw new Exception("Unsupported Image Format!");
  }
}
?>

3 个答案:

答案 0 :(得分:0)

更改

echo '<p class="games_image_result"><img src="<?php echo $row->image; ?>" width="175"  height="200" />';

echo '<p class="games_image_result"><img src="' . $row->image . '" width="175"  height="200" />';

编辑:编辑完问题后......

您正在将blob,原始数据存储到数据库中。要在您的页面上显示图片,您需要将base64编码数据输入到图像标记的src中。

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

答案 1 :(得分:0)

while($row=mysql_fetch_array($run_posts))
   {?>
 <p class="games_image_result"><img src="<?php echo $row->image; ?>" width="175"  height="200" />';
<p class="games_content_result"><?php echo $row['body'];?>

<?php  }

答案 2 :(得分:0)

如果您将图像存储为blob,则需要使用base64:

    <img src="data:image/jpeg;base64,<?= base64_encode($row['image']) ?>"/>

当您使用数组时,不知道为什么要尝试使用$ row-&gt;图像。

如果您只是存储需要更改的路径:

    <img src="<?php echo $row->image; ?>" width="175"  height="200" />';

为:

    <img src="<?php echo $row['image']; ?>" width="175"  height="200" />';

编辑2:

你的服务器配置没问题,你的代码到处都是错误的。您似乎不了解基本的字符串语法。不要使用php来打印你的HTML,这是不可读的......试试这个:

    <?php
    include('connects.php');
    $select_posts = "select * from testblob";
    $run_posts = mysql_query($select_posts);

    while($row=mysql_fetch_array($run_posts))
    { ?>
<p class="games_image_result"><img src="<?php echo $row['image']; ?>" width="175"  height="200" />
<p class="games_content_result"><?php echo $_content = $row['body']; }
    ?>
相关问题