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

如何使用php显示数据库中的图像,php,mysql,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'];

我试图显示来自数据库的图像,但无法显示该图像。但它的显示方式如下
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;
}

将您的
$image
放入html的
img
标记中

试试这个

echo '<img src="your_path_to_image/'.$image.'" />';
到图像的路径应该是图像文件夹的绝对路径,例如:
/home/son/public\u html/images/
,或者作为服务器上的文件夹结构

更新2:

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

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

您需要执行此操作才能显示图像

$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>";
} 
$sqlimage=“从userdetail中选择图像,其中`id`=$id1”;
$imageresult1=mysql\u查询($sqlimage);
而($rows=mysql\u fetch\u assoc($imageresult1))
{
$image=$rows['image'];
回声“;
回声“
”; }
您需要使用html img标记。

只需替换即可

print $image;

echo';

而不是
打印$image你应该选择
打印“>”

请注意,$image应该包含图像的路径

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

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


$sqlimage=“从userdetail中选择图像,其中`id`=$id1”;
$imageresult1=mysqli_查询($link,$sqlimage);
而($rows=mysqli\u fetch\u assoc($imageresult1))
{
回声“;
} 
显示MySql数据库中的图像

$db=mysqli_connect(“localhost”、“root”、“”、“DbName”);
$sql=“从id=$id的产品中选择*”;
$sth=$db->query($sql);
$result=mysqli\u fetch\u数组($sth);
回声';


有时会出现问题,因为mysql服务器的端口号不正确,为了避免出现问题,只需使用主机名写端口号,如“localhost:3306”
若你们在同一个系统上安装了两个mysql服务器,那个么就根据这个写端口
为了显示数据库中的任何数据,请确保执行以下步骤
1.与sql的正确连接
2.选择数据库
3.写查询
4.在查询中写入正确的表名
5.最后是数据遍历

将此代码放入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>";
}
$sql=“从用户详细信息中选择*”;
$result=mysqli_查询(“连接”,$sql);
而($row=mysqli\u fetch\u数组($result,mysqli\u两者)){
回声“;
回显“”$row['text']。“

”; }

我希望这是可行的。

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

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

while($rows = mysql_fetch_assoc($imageresult1))
{       
    $image = $rows['image'];    
    print $image;
}
这里我显示的是数据库中的图像和名称

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

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>

在PHP中显示图像-Campuslife
正文{背景色:#f2f2f2;颜色:#333;}
.main{box shadow:0.125rem.25remrgba(0,0,0,075)!重要;页边距顶部:10px;}
h3{背景色:#4294D1;颜色:#f7f7f7;填充:15px;边框半径:4px;方框阴影:0 1px 6px rgba(57,73,76,0.35);}
.img框{页边距顶部:20px;}
.img块{float:left;margin right:5px;text align:center;}
p{margin top:0;}
img{宽度:375px;最小高度:250px;边距底部:10px;方框阴影:0.125rem.25rem rgba(0,0,0,075)!重要;边框:6px实心35; F7F7F7F7;}
显示来自数据库的图像
“alt=”“title=”“width=“300”height=“200”class=“img响应”/>

如果您发现任何错误,那么您可以直接按照我从何处找到的教程进行操作。你可以在这个网站上一步一步地看到实时教程

我希望你会喜欢我的回答

多谢各位

输出

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


提示:请不要使用mysql函数,因为它们已被弃用。您可以使用mysqli或PDO。用于避免那些恼人的SQL注入。您的问题不清楚您的数据库中是有图像还是只有文件名。@markust i我的数据库显示的是user-1.jpg,您能告诉我如何将图像保存在数据库中吗databse@Xavi因此,数据库只存储文件名。这是存储文件的一种有效方法。你已经有答案了。在数据库中存储图像是另一个问题。如果您对答案感兴趣,请使用谷歌。您使用ans的原因已更新!伟大的现在它可能有助于问题的所有者从未以这种方式呈现文件名,考虑把它放在引号之间(这一次双引号)。虽然规范允许缺少引号,但如果缺少关闭的标记,并且文件名包含“/”,则可能会遇到麻烦。您通常应该在答案中包含解释,而不仅仅是代码。否则,它可能并不总是清楚它为什么有效(或者是否有效)。此外,只有代码而没有解释的答案通常会被否决和删除。请编辑您的帖子并在代码中添加解释。我希望,这个答案对您有所帮助。请访问以下网站获取更多教程。
$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'].'" />';


} 
$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'] ).'"/>';
<?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'].'">';
    }
    ?>
<?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
$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>";
}
   <!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>