Tôi đang cố gắng hiển thị 5 hình ảnh cuối cùng được tải lên bảng "cửa hàng" của tôi trong MySql. Tôi là một noob hoàn chỉnh với PHP và cơ sở dữ liệu và tôi đã đọc rất nhiều về cách thực hiện điều này nhưng không có may mắn.Làm thế nào để hiển thị hình ảnh BLOB được lưu trữ trong cơ sở dữ liệu MySql?
Tôi có thể lưu trữ và hiển thị từng ảnh một nhưng tôi muốn có thể có một bộ sưu tập các loại để hiển thị 5 ảnh được tải lên gần đây nhất.
bất kỳ lời khuyên hoặc trợ giúp nào sẽ được đánh giá cao nhờ!
p.s. Tôi biết nó cau mày khi lưu trữ hình ảnh vào một cơ sở dữ liệu như thế này nhưng dự án này chỉ là để thực hành.
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;
?>
Kiểm tra này : http://stackoverflow.com/questions/118144/whats-the-most-efficient-way-to-select-the-last-n-rows-in-a-table-without-chang – Vucko