简体   繁体   English

如何从数据库中检索图像并在网页上显示图像

[英]How to retrieve image from database and display image on the web page

I have inserted image into database and store name in the table.我已将图像插入数据库并将名称存储在表中。 my image is saved in a folder named 'Uploads'.我的图像保存在名为“上传”的文件夹中。 Now need to retrieve image from the databse and display it.现在需要从数据库中检索图像并显示它。 when I try to display It only shows the image name which is taken from my table.but it does not show the image.当我尝试显示时,它只显示从我的表中获取的图像名称。但它不显示图像。

retrieving code is given below检索代码如下

$sql="SELECT * FROM candi_profile WHERE can_email='{$_SESSION['usr_email']}'";

$result=mysqli_query($con,$sql);
if(!$result) die(mysqli_error($con));
  <div class="container">

        <!-- Page Header -->
        <div class="row">
            <div class="col-lg-12">
                <h1 class="page-header">Employer Dashboard 
                   
                </h1>
            </div>
        </div>
        <!-- /.row -->

        <!-- Projects Row -->
        
        <div class="row">
            <div class="col-md-4">
        <?php
          while($rows=mysqli_fetch_array($result)){
              $c_id = $rows['can_id'];
            var_dump($c_id);
             ?>
             
            <p class="lead"><?php echo $rows['can_name'] ?></p>
            <div class="profile-sidebar">
                <!-- SIDEBAR USERPIC -->
                
                <div class="profile-userpic">
                        <p class="lead">
                         <?php echo $rows['pic_name'] ?></p>
                </div>
                
                <!-- END SIDEBAR USERPIC -->

                <!-- SIDEBAR USER TITLE -->
                <div class="profile-usertitle">
                    <div class="profile-usertitle-name">
                        Marcus Doe
                    </div>
                    <div class="profile-usertitle-job">
                       <?php echo $rows['can_city'] ?>
                   <i class="glyphicon glyphicon-map-marker">
                        </i>
                    </div>
                        
                    <div class="profile-usertitle-job">
                         <i class="glyphicon glyphicon-envelope"></i>
                      <?php echo $rows['can_email'] ?>
                    </div>
                        
                    <div class="profile-usertitle-job">
                        <?php echo $rows['can_country'] ?>
                    </div>
                </div>
                <!-- END SIDEBAR USER TITLE -->
                
                <!-- SIDEBAR BUTTONS -->
                <div class="profile-userbuttons">
                    <hr>
                </div>
                
                <!-- END SIDEBAR BUTTONS -->
                <!-- SIDEBAR MENU -->
      
            <?php
            }
            ?>          
            </div>

在此处输入图片说明 在此处输入图片说明

使用 image 标签显示图像并为其指定图像文件夹的路径

<img src="your path/<?php echo $rows['pic_name'] ?>" />

I assume that the content of $rows['pic_name'] is string only as said on your question.我假设$rows['pic_name'] 的内容只是你的问题中所说的字符串。

Put an image attribute and call the path of the image with the corresponding filename save on the database.放置一个图像属性,并调用保存在数据库中的相应文件名的图像路径。

<img src = "<path>/<?php echo $rows['pic_name'] ?>" />

NOTE :注意

Make sure the image is existing on your desire path.确保图像存在于您想要的路径上。

you can use this code to retrieve image from database您可以使用此代码从数据库中检索图像

<?php
include 'connection.php'
?>
<?php

$result = mysql_query("SELECT * FROM table") or die(mysql_error()); 


?>

<table border="1" cellpadding="5" cellspacing="5">
<tr> <th>Image</th></tr>

<?php

while($row = mysql_fetch_array($result)) {

$id = $row['id'];

?>
    <tr>

        <td><img src="uploads/<?php echo $row['pic_name'];?>" alt=" " height="75" width="75"></td>

   </tr>

<?php   
} 
}

?>
</table>

friend instead of making images folder you should make a new image column(ie "imageColumn ") type as blob then You need to create another php script to return the image data, eg getImage.php.朋友,而不是制作图像文件夹,您应该创建一个新的图像列(即“imageColumn”)类型为 blob 然后您需要创建另一个 php 脚本来返回图像数据,例如 getImage.php。

home.php(or display image page) code home.php(或显示图片页面)代码

<body>
<img src="getImage.php?id=1" width="175" height="200" />
</body>

Then getImage.php is然后 getImage.php 是

<?php

  $id = $_GET['id'];
  // do some validation here to ensure id is safe

  $link = mysql_connect("localhost", "root", "");
  mysql_select_db("dvddb");
  $sql = "SELECT imageColumn FROM Tablename WHERE id=$id";
  $result = mysql_query("$sql");
  $row = mysql_fetch_assoc($result);
  mysql_close($link);

  header("Content-type: image/jpeg");
  echo $row['imageColumn '];
?>
  1. First fetch image from database using query首先使用查询从数据库中获取图像
  2. The imagejpeg() function is an inbuilt function in PHP which is used to display image to browser or file. imagejpeg() 函数是 PHP 中的一个内置函数,用于向浏览器或文件显示图像。
  3. Get data using function ob_get_contents();使用函数 ob_get_contents() 获取数据;
  4. Display image in page with height and width在页面中显示具有高度和宽度的图像

$id = $_GET['id'];    
$sql = "select image from table where id='".$id."'";    
$res = mysqli_query($sql);    
$row = mysqli_fetch_assoc($res);    

$image = $row['image'];    
ob_start();    
imagejpeg($image, null, 50);        
$data = ob_get_contents();    
ob_end_clean();    
echo "<img src='data:image/jpg;base64,'".base64_encode($data)."' style='border:1px 
black; border-radius:10px; width:100px; height:125px;'>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM