php 如何从 MySQL 数据库中检索图像并显示在 html 标签中

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

How to retrieve images from MySQL database and display in an html tag

phpmysqlhtml

提问by exxcellent

I created a MySQL database with a table using phpmyadmin. I created this table with a BLOB column to hold a jpeg file.

我使用 phpmyadmin 创建了一个带有表的 MySQL 数据库。我用一个 BLOB 列创建了这个表来保存一个 jpeg 文件。

I have issues with regards to the php variable $resulthere.

我有关于 php 变量的$result问题。

My code so far: (catalog.php):

到目前为止我的代码:(catalog.php):

<body>
<?php
  $link = mysql_connect("localhost", "root", "");
  mysql_select_db("dvddb");
  $sql = "SELECT dvdimage FROM dvd WHERE id=1";
  $result = mysql_query("$sql");
  mysql_close($link);

?>
<img src="" width="175" height="200" />
</body>

How can I get the variable $result from PHP into the HTML so I can display it in the <img>tag?

如何将变量 $result 从 PHP 获取到 HTML 中,以便我可以在<img>标签中显示它?

回答by daiscog

You can't. You need to create another php script to return the image data, e.g. getImage.php. Change catalog.php to:

你不能。您需要创建另一个 php 脚本来返回图像数据,例如 getImage.php。将 catalog.php 更改为:

<body>
<img src="getImage.php?id=1" width="175" height="200" />
</body>

Then getImage.php is

然后 getImage.php 是

<?php

  $id = $_GET['id'];
  // do some validation here to ensure id is safe

  $link = mysql_connect("localhost", "root", "");
  mysql_select_db("dvddb");
  $sql = "SELECT dvdimage FROM dvd WHERE id=$id";
  $result = mysql_query("$sql");
  $row = mysql_fetch_assoc($result);
  mysql_close($link);

  header("Content-type: image/jpeg");
  echo $row['dvdimage'];
?>

回答by Ilmari Karonen

Technically, you cantoo put image data in an img tag, using data URIs.

从技术上讲,您也可以使用数据 URI将图像数据放入 img 标签中。

<img src="data:image/jpeg;base64,<?php echo base64_encode( $image_data ); ?>" />

There are some special circumstances where this could even be useful, although in most cases you're better off serving the image through a separate script like daiscog suggests.

在某些特殊情况下,这甚至可能很有用,尽管在大多数情况下,您最好通过像 daiscog 建议的单独脚本来提供图像。

回答by James Williams

You need to retrieve and disect the information into what you need.

您需要检索信息并将其分解为您需要的信息。

while($row = mysql_fetch_array($result)) {
 echo "img src='",$row['filename'],"' width='175' height='200' />";
}

回答by Marcus

First off you need to fetch the resulting row from the resultset of the query. For that you can use mysql_fetch_row. Now that you have the fetched row you can access the retrieved value and echo it into the src.

首先,您需要从查询的结果集中获取结果行。为此,您可以使用mysql_fetch_row. 现在您有了获取的行,您可以访问检索到的值并将其回显到 src 中。

For example:

例如:

$sql = "SELECT dvdimage FROM dvd WHERE id=1";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
?>
<img src="<?=$row[0]?>" width="175" height="200" />
<?

回答by Punit

add $row = mysql_fetch_object($result);after your mysql_query();

$row = mysql_fetch_object($result);在您的 mysql_query() 之后添加;

your html <img src="<?php echo $row->dvdimage; ?>" width="175" height="200" />

你的html <img src="<?php echo $row->dvdimage; ?>" width="175" height="200" />

回答by Ganesh

I have added slashes before inserting into database so on the time of fetching i removed slashes again stripslashes()and it works for me. I am sharing the code which works for me.

我在插入数据库之前添加了斜杠,因此在获取时我再次删除了斜杠stripslashes(),它对我有用。我正在分享对我有用的代码。

How i inserted into mysql db (blob type)

我如何插入 mysql db(blob 类型)

$db = mysqli_connect("localhost","root","","dName"); 
$image = addslashes(file_get_contents($_FILES['images']['tmp_name']));
$query = "INSERT INTO student_img (id,image) VALUES('','$image')";  
$query = mysqli_query($db, $query);

Now to access the image

现在访问图像

$sqlQuery = "SELECT * FROM student_img WHERE id = $stid";
$rs = $db->query($sqlQuery);
$result=mysqli_fetch_array($rs);
echo '<img src="data:image/jpeg;base64,'.base64_encode( stripslashes($result['image']) ).'"/>';

Hope it will help someone

希望它会帮助某人

Thanks.

谢谢。