我试图显示上传到MySql的“商店”表中的最后5张图片。 我是一个完全的PHP和数据库noob,我一直在阅读很多关于如何做到这一点,但没有运气。如何显示存储在MySql数据库中的BLOB图像?
我可以一次存储和显示一张照片,但我希望能够有一个排序库以显示最近5次上传的图片。
任何意见或帮助将不胜感激谢谢!
p.s.我知道它不喜欢把图片存储到这样的数据库,但这个项目只是为了练习。
的index.php
<!DOCTYPE html>
<html>
<head>
<title>Project One</title>
</head>
<body>
<form action="index.php" method="POST" enctype="multipart/form-data">
File:
<input type="file" name="image"> <input type="submit" value="Upload">
<form>
<p />
<?php
//connect to database
(connect to server)
(select correct DB)
//file properties
$file = $_FILES['image']['tmp_name'];
if (!isset($file))
echo "please select an image.";
else
{
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = $_FILES['image']['name'];
$image_size = getimagesize($_FILES['image']['tmp_name']);
if($image_size==FALSE)
echo "That's not an image.";
else
{
if (!$insert = mysql_query("INSERT INTO store VALUES ('', '$image_name', '$image')"))
echo "Problem Uploading Image.";
else
{
$lastid = mysql_insert_id();
echo "Image uploaded. <p />Your image:<p /><img src=get.php?id=$lastid>";
}
}
}
?>
<p />
<p />
<a href="http://WEBSITE.com/gallery.php"> Go to Gallery </a>
</body>
</html>
get.php
<?php
//connect to database
(connect to server)
(select correct DB)
$id = addslashes($_REQUEST['id']);
$image = mysql_query("SELECT * FROM store WHERE id=$id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];
header("Content-type: image/jpeg");
echo $image;
?>
检查这: http://stackoverflow.com/questions/118144/whats-the-most-efficient-way-to-select-the-last-n-rows-in-a-table-without-chang – Vucko