简体   繁体   English

从mysql在php中显示图像

[英]displaying image in php from mysql

I searched for the solutions but they didn't work!我搜索了解决方案,但没有奏效! I am trying to show the image that is in my database [in Eventss table] the row is called image and it is BLOB format.我试图显示我的数据库中的图像 [在 Eventss 表中] 该行称为图像,它是 BLOB 格式。 This is the page where the data will be displayed for people and I am focusing on the php know and I will fix the if condition after figuring out how to show the image.这是为人们显示数据的页面,我专注于 php 知识,我将在弄清楚如何显示图像后修复 if 条件。

<?php 
mysql_connect("***", "***", "***")
or die("<p>Error connecting to database: " . mysql_error() . "</p>");

mysql_select_db("project")
or die("<p>Error selecting the database ectoday: " . mysql_error() . "</p>");

session_start();
$select_query = "SELECT * FROM Eventss WHERE id=". $_SESSION["event_id"];
$result = mysql_query($select_query);
if($result) 
    { 
        if (mysql_num_rows($result) > 0)
        {   
            while($row = mysql_fetch_assoc($result))
            {
                echo $row['title']. "<br>";
                if ($row['image'] !== NULL)
                {
                    $poster =  $row['image'];
                    echo $row['content']. "<br>";
                    echo $row['event_date']. "  [" . $row['event_time']. "]"."<br>";
                    echo $row['location']. "<br>";
                }
                else
                {
                echo $row['content']. "<br>";
                echo $row['event_date']. "  [" . $row['event_time']. "]"."<br>";
                echo $row['location']. "<br>";
                }

            }   
        }
        else
        {
            echo "Sorry, there is no more data for this event";
        }
    }
 ?>
 <html>
 <head><head>
 <body>
    <img src="<?php echo $poster; ?>" width="175" height="200" />
  </body>
 </html>

If it's stored in a BLOB it's most likely the actual image data as binary, instead of a reference to an image on the filesystem.如果它存储在 BLOB 中,它很可能是二进制的实际图像数据,而不是对文件系统上图像的引用。 You will either have to write it to a file and display it using a standard img tag, or otherwise convert it to base64 (see base64_encode() )...您要么必须将其写入文件并使用标准 img 标签显示它,要么将其转换为 base64(请参阅base64_encode() )...

<img src="data:image/jpeg;base64, HereGoesYourBase64fromBLOB..." />

...but be aware that base64-encoded image data may not be the most bandwidth-friendly solution. ...但请注意,base64 编码的图像数据可能不是对带宽最友好的解决方案。 It's better to have static resources stored in the filesystem.最好将静态资源存储在文件系统中。

Assuming that you don't store a path to that image, but the image content itself and that it is jpg (you can change to png or whatever type you have there).假设您不存储该图像的路径,而是存储图像内容本身并且它是 jpg(您可以更改为 png 或您拥有的任何类型)。 If so, use data URI syntax for src:如果是这样,请对 src 使用数据 URI 语法:

<img src="<?php 
echo 'data:image/jpg;base64,'.base64_encode($poster); ?>" width="175" height="200" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM