【发布时间】:2014-08-05 01:48:34
【问题描述】:
我已经使用 PHP 将一些图像上传到 MySQL。每次修改 HTML img 标签中的 id 时,我也可以显示 1 张图像。 但现在我正在尝试显示存储在 MySql 数据库中的所有图像,问题是当我使用“While Loop”时,它只显示文本列,而不显示存储为 MySQL 中 BLOB 数据的图像......
我有一个名为:my_db 的数据库 我有一个名为:blob 的表 我的表中有 3 列:id、name & image
这里是 index.php 的代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form action="index.php" method="POST" enctype="multipart/form-data">
<input type="file" name="image"><input type="submit" name="submit" value="Upload">
</form>
<?php
if(isset($_POST['submit']))
{
mysql_connect("127.0.0.1","root","");
mysql_select_db("my_db");
$imageName = mysql_real_escape_string($_FILES["image"]["name"]);
$imageData = mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"]));
$imageType = mysql_real_escape_string($_FILES["image"]["type"]);
if(substr($imageType,0,5) == "image")
{
mysql_query("INSERT INTO `blob` VALUES('','$imageName','$imageData')");
echo "Image Uploaded!";
}
else
{
echo "Only images are allowed!";
}
}
?>
<img src="showimage.php?id=11">
</body>
</html>
这里是 showimage.php 的代码:
<?php
mysql_connect("127.0.0.1","root","");
mysql_select_db("my_db");
if(isset($_GET['id']))
{
$id = mysql_real_escape_string($_GET['id']);
$query = mysql_query("SELECT * FROM `blob` WHERE `id`='$id'");
while($row = mysql_fetch_assoc($query))
{
$imageData = $row["image"];
}
header("content-type: image/jpeg");
echo $imageData;
}
else
{
echo "Error!";
}
?>
提前致谢:-)
【问题讨论】: