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

12

我试图显示从数据库中获取的图像,但是我无法显示该图像。它显示为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;
}

2
提示:请不要使用mysql_*函数,因为它们已经被弃用。您可以使用mysqli或PDO。 - HddnTHA
3
使用PDO预处理语句来避免SQL注入攻击。您的问题不清楚,您的数据库中是否有图像或仅有文件名。 - Markus Malkusch
@markust 我的数据库中显示 user-1.jpg,请问您能告诉我如何将图片保存在数据库中吗? - Xavi
1
@Xavi 所以你的数据库只存储文件名。这是一种有效的存储文件的方法。你已经得到了答案。将图像存储在数据库中是一个不同的问题。如果你对答案感兴趣,可以使用谷歌搜索。 - Markus Malkusch
10个回答

21

从 MySql 数据库中显示图像。

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

4
例如,如果您使用此代码,您可以从数据库(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
<?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'].'">';
    }
    ?>

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

我希望这个能够工作。


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

请编辑您的帖子并添加代码说明。 - juzraai

0
$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'].'" />';


} 

通常你的回答应该包含解释,而不仅仅是代码。否则,它可能不总是清楚为什么它能够工作(或者是否能够工作)。此外,没有解释的仅有代码的回答经常会被投票降低并删除。 - sideshowbarker

0

你需要这样做来显示图片

$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 标签。

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

0

简单地替换

print $image;

使用

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

为什么要在 echo 中使用 <?php? - HddnTHA
答案已更新! - Mehar
太好了。现在它可能会帮助问题的所有者了。 - HddnTHA
永远不要以这种方式渲染文件名,考虑将其放在引号(这次是双引号)之间。尽管规范允许缺少引号,但如果遗漏了关闭标签,而文件名包含“/”,就会遇到麻烦。 - user1299518

0

代替 print $image;,你应该选择 print "<img src=<?$image;?>>"

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接