如何从 MySQL 数据库中检索图像并显示在 html 标签中 [英] How to retrieve images from MySQL database and display in an html tag

查看:42
本文介绍了如何从 MySQL 数据库中检索图像并显示在 html 标签中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

I have issues with regards to the php variable $result here.

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

My code so far: (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>

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

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

推荐答案

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

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

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

那么getImage.php就是

Then getImage.php is

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

这篇关于如何从 MySQL 数据库中检索图像并显示在 html 标签中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆