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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38560752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:37:49  来源:igfitidea点击:

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

phpmysql

提问by Deepashika

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>

enter image description hereenter image description here

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

回答by Owais Aslam

Use image tag to display the image and give it path to the image folder

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

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

回答by Edmar

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.

确保图像存在于您想要的路径上。

回答by Vamsi Abbineni

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>

回答by Hassan Saeed

friend instead of making images folder you should make a new image column(i.e "imageColumn ") type as blob then You need to create another php script to return the image data, e.g. 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 '];
?>