php 如何使用php显示数据库中的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23842268/
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-25 16:59:49  来源:igfitidea点击:

How to display image from database using php

phpmysql

提问by Xavi

I am trying to display an image coming from the database and I was not able to display the image .but its showing like this user-1.jpgPlease see my code can one guide me how to display the image.

我试图显示来自数据库的图像,但我无法显示图像。但它的显示方式是这样的user-1.jpg请看我的代码可以指导我如何显示图像。

$sqlimage = "SELECT image FROM userdetail where `id` = $id1";
$imageresult1 = mysql_query($sqlimage);

while($rows = mysql_fetch_assoc($imageresult1))
{       
    $image = $rows['image'];    
    print $image;
}

回答by Charles

Displaying an image from MySql Db.

显示来自 MySql Db 的图像。

$db = mysqli_connect("localhost","root","","DbName"); 
$sql = "SELECT * FROM products WHERE id = $id";
$sth = $db->query($sql);
$result=mysqli_fetch_array($sth);
echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image'] ).'"/>';

回答by MANZAR HUSSAIN

If you want to show images from database then first you have to insert the images in database then you can show that image on page. Follow the below code to show or display or fetch the image.

如果要显示数据库中的图像,则首先必须将图像插入数据库中,然后才能在页面上显示该图像。按照以下代码显示或显示或获取图像。

Here I am showing image and name from database.

在这里,我显示了数据库中的图像和名称。

Note:I am only storing the pathof image in database but actual image i am storing in photofolder.

注意:我只将图像的路径存储在数据库中,但实际图像存储在照片文件夹中。

PHP Complete Code with design: show-code.php

PHP 完整代码设计:show-code.php

   <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, intial-scale=1.0"/>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<title>Show Image in PHP - Campuslife</title>
<style>
    body{background-color: #f2f2f2; color: #333;}
    .main{box-shadow: 0 .125rem .25rem rgba(0,0,0,.075)!important; margin-top: 10px;}
    h3{background-color: #4294D1; color: #f7f7f7; padding: 15px; border-radius: 4px; box-shadow: 0 1px 6px rgba(57,73,76,0.35);}
    .img-box{margin-top: 20px;}
    .img-block{float: left; margin-right: 5px; text-align: center;}
    p{margin-top: 0;}
    img{width: 375px; min-height: 250px; margin-bottom: 10px; box-shadow: 0 .125rem .25rem rgba(0,0,0,.075)!important; border:6px solid #f7f7f7;}
</style>
</head>
    <body>
    <!-------------------Main Content------------------------------>
    <div class="container main">
        <h3>Showing Images from database</h3>
        <div class="img-box">
    <?php
        $host ="localhost";
        $uname = "root";
        $pwd = '123456';
        $db_name = "master";

        $file_path = 'photo/';
        $result = mysqli_connect($host,$uname,$pwd) or die("Could not connect to database." .mysqli_error());
        mysqli_select_db($result,$db_name) or die("Could not select the databse." .mysqli_error());
        $image_query = mysqli_query($result,"select img_name,img_path from image_table");
        while($rows = mysqli_fetch_array($image_query))
        {
            $img_name = $rows['img_name'];
            $img_src = $rows['img_path'];
        ?>

        <div class="img-block">
        <img src="<?php echo $img_src; ?>" alt="" title="<?php echo $img_name; ?>" width="300" height="200" class="img-responsive" />
        <p><strong><?php echo $img_name; ?></strong></p>
        </div>

        <?php
        }
    ?>
        </div>
    </div>
    </body>
</body>
</html>

If you found any mistake then you can directly follow the tutorial which is i found from where. You can see the live tutorial step by step on this website.

如果您发现任何错误,那么您可以直接按照我从哪里找到的教程进行操作。您可以在本网站上逐步查看实时教程。

I hope may be you like my answer.

我希望你会喜欢我的回答。

Thank You

谢谢你

https://www.campuslife.co.in/Php/how-to-show-image-from-database-using-php-mysql.php

https://www.campuslife.co.in/Php/how-to-show-image-from-database-using-php-mysql.php

Output

输出

[Showing Images from Database][1]

[显示数据库中的图像][1]

https://www.campuslife.co.in/Php/image/show-image1.png

https://www.campuslife.co.in/Php/image/show-image1.png

回答by Yo Ne S

For example if you use this code , you can load image from db (mysql) and display it in php5 ;)

例如,如果您使用此代码,则可以从 db (mysql) 加载图像并将其显示在 php5 中;)

<?php
   $con =mysql_connect("localhost", "root" , "");
   $sdb= mysql_select_db("my_database",$con);
   $sql = "SELECT * FROM `news` WHERE 1";
   $mq = mysql_query($sql) or die ("not working query");
   $row = mysql_fetch_array($mq) or die("line 44 not working");
   $s=$row['photo'];
   echo $row['photo'];

   echo '<img src="'.$s.'" alt="HTML5 Icon" style="width:128px;height:128px">';
   ?>

回答by Paulo Rodrigues

$sqlimage  = "SELECT image FROM userdetail where `id` = $id1";
    $imageresult1 = mysqli_query($link, $sqlimage);

    while($rows=mysqli_fetch_assoc($imageresult1))
{

    echo "<img src = 'Image/".$row['image'].'" />';


} 

回答by Muhammad Raheel

You need to do this to display image

您需要这样做才能显示图像

$sqlimage  = "SELECT image FROM userdetail where `id` = $id1";
$imageresult1 = mysql_query($sqlimage);

while($rows=mysql_fetch_assoc($imageresult1))
{
    $image = $rows['image'];
    echo "<img src='$image' >";
    echo "<br>";
} 

You need to use html img tag.

您需要使用 html img 标签。

回答by Lizwi

<?php
       $connection =mysql_connect("localhost", "root" , "");
       $sqlimage = "SELECT * FROM userdetail where `id` = '".$id1."'";
      $imageresult1 = mysql_query($sqlimage,$connection);

      while($rows = mysql_fetch_assoc($imageresult1))
    {       
       echo'<img height="300" width="300" src="data:image;base64,'.$rows['image'].'">';
    }
    ?>

回答by saurabh

<?php
    $conn = mysql_connect ("localhost:3306","root","");
    $db = mysql_select_db ("database_name", $conn);

    if(!$db) {
        echo mysql_error();
    }

    $q = "SELECT image FROM table_name where id=4";
    $r = mysql_query ("$q",$conn);
    if($r) {
         while($row = mysql_fetch_array($r)) {
            header ("Content-type: image/jpeg");       
    echo $row ["image"];
        }
    }else{
        echo mysql_error();
    }
    ?>

sometimes problem may  occures because of port number of mysql server is incoreect to avoid it just write port number with host name like this "localhost:3306" 
in case if you have installed two mysql servers on same system then write port according to that

in order to display any data from database please make sure following steps
1.proper connection with sql
2.select database
3.write query 
4.write correct table name inside the query
5.and last is traverse through data

回答by Ali Shahbaz

put this code to your php page.

将此代码放入您的php页面。

$sql = "SELECT * FROM userdetail";
$result = mysqli_query("connection ", $sql);

while ($row = mysqli_fetch_array($result,MYSQLI_BOTH)) {
    echo "<img src='images/".$row['image']."'>";
    echo "<p>".$row['text']. "</p>";
}

i hope this is work.

我希望这是工作。

回答by Mehar

Simply replace

只需更换

print $image;

with

 echo '<img src=".$image." >';

回答by user2656474

instead of print $image;you should go for print "<img src=<?$image;?>>"

而不是print $image;你应该去print "<img src=<?$image;?>>"

and note that $image should contain the path of your image.

并注意 $image 应包含图像的路径。

So, If you are only storing the name of your image in database then instead of that you have to store the full path of your image in the database like /root/user/Documents/image.jpeg.

因此,如果您仅将图像名称存储在数据库中,那么您必须将图像的完整路径存储在数据库中,例如 /root/user/Documents/image.jpeg。