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

时间:2014-05-24 07:07:49

标签: php mysql

我正在尝试显示来自数据库的图像,但我无法显示图像。但是它显示为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;
}

11 个答案:

答案 0 :(得分:10)

  

从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'] ).'"/>';

答案 1 :(得分:3)

例如,如果您使用此代码,您可以从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">';
   ?>

答案 2 :(得分:3)

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

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

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

带有设计的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>

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

希望您能喜欢我的答案。

谢谢

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

输出

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

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

答案 3 :(得分:1)

您需要这样做以显示图像

$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>";
} 

您需要使用html img标记。

答案 4 :(得分:1)

{{1}}

答案 5 :(得分:1)

<?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'].'">';
    }
    ?>

答案 6 :(得分:0)

$image放入html的img标记

试试这个

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

而不是

print $image;

your_path_to_image将是您的图片文件夹的绝对路径,例如:/home/son/public_html/images/或服务器上的文件夹结构。

更新2:

如果您的图像位于存在此页面文件的同一文件夹中 你可以用这个

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

答案 7 :(得分:0)

简单地替换

print $image;

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

答案 8 :(得分:0)

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

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

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

答案 9 :(得分:0)

<?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

答案 10 :(得分:0)

将此代码放入您的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>";
}

我希望这是有效的。