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

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

问题描述

我使用phpmyadmin创建了一个带有表的MySQL数据库。



我对php变量 $ result

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

 

code><身体GT;
<?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 =175height =200/>
< / body>

如何从PHP获取变量$ result到HTML中,以便将其显示在< img> tag?

解决方案

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

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

然后getImage.php为

 <?php 

$ id = $ _GET ['id'];
//在这里做一些验证以确保id安全

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


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

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

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>

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

解决方案

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>

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天全站免登陆