[英]I am trying to display an image from MYSQL via PHP in an HTML file. What is wrong with my code?
Here is the PHP code. 这是PHP代码。 What I'm basically trying to do is display a single image from a mySQL database, but for the life of me I can't seem to figure it out. 我基本上想做的是显示来自mySQL数据库的单个图像,但是对我而言,我似乎无法弄清楚。
<?php
$port = "****";
$server = "****".$port;
$dbname ="****";
$user = "****";
$conn = mysql_connect ("$server", "$user", "$pass") or die ("Connection
Error or Bad Port");
mysql_select_db($dbname) or die("Missing Database");
?>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en"
"http://www.w3.org/TR/REC-html40/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
/>
<title>Query 3</title>
</head>
<body bgcolor="white">
<hr>
<?php
$speakerPic = $_POST['speakerPic'];
$query = "SELECT speaker.speaker_picture FROM Speaker JOIN Contact USING(contact_id)
WHERE Contact.lname = ";
$query = $query."'".$speakerPic."';";
?>
<p>
The query:
<p>
<?php
print $query;
?>
<hr>
<p>
Result of query:
</p>
<?php
$row = mysql_fetch_array($query);
$content = $row['image'];
header('Content-type: image/jpg');
echo $content;
?>
</body>
</html>
What isn't working here? 这里什么不工作? I really do appreciate your help. 非常感谢您的帮助。
To include an image in an HTML document you use an <img>
tag with a src
attribute that contains a URI pointing at the image. 要将图像包含在HTML文档中,请使用<img>
标记,该标记的src
属性包含指向该图像的URI。
That could be an HTTP URI (which could hit a PHP script that gets the image data from a database and returns it) and it could be a DATA URI with the image encoded directly into the page. 可以是HTTP URI(可以打入PHP脚本,该脚本从数据库获取图像数据并返回),也可以是数据URI ,图像直接编码到页面中。
The usual approach to dealing with images is to keep them on a filesystem and store URLs (or file paths that URLs can be constructed from) in the database. 处理图像的常用方法是将它们保留在文件系统上,并将URL(或可以构造URL的文件路径)存储在数据库中。
You can't just spit out an HTTP response in the middle of another HTTP response as you are trying to do now. 您不能像现在尝试那样在另一个HTTP响应中间吐出一个HTTP响应。
You need to call mysql_query before you call mysql_fetch_array . 您需要先调用mysql_query,然后再调用mysql_fetch_array 。
$result = mysql_query( $query);
$row = mysql_fetch_array($result);
Also, you should sanitize $speakerPic
like so: 另外,您应该这样清理$speakerPic
:
$query = $query."'". mysql_real_escape_string( $speakerPic)."';";
您没有名为“图片”的行,请尝试像这样在查询中重新命名您的行:
SELECT speaker.speaker_picture AS image FROM ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.