我想知道如何在 php 文档上显示多张来自数据库的图片。我知道将图片放入 mysql 并取出其中一个,但我不知道如何取出多个(或全部)。提前致谢。
我的一个 php 文件看起来像这样
<html>
<head>
<title>Upload</title>
</head>
<form action='pictureuploadtest.php' method='POST' enctype='multipart/form-data'>
File: <input type='file' name='fileone'>
<input type = 'submit' name='submitfile'>
</form>
<?php
$con = mysql_connect("localhost", "username", "password") or die("Cannot connect: ".mysql_error());
mysql_select_db("testpicture") or die("Cannot connect to db: ".mysql_error());
$file = $_FILES['fileone']['tmp_name'];
if(!isset($file)) {
print "Choose an image";
} else {
$image = addslashes(file_get_contents($_FILES['fileone']['tmp_name']));
$imagename = addslashes($_FILES['fileone']['name']);
$imagesize = getimagesize($_FILES['fileone']['tmp_name']);
if($imagesize === false) {
echo "Invalid image.";
} else {
$insert = "INSERT INTO upload VALUES ('', '$imagename', '$image')";
if(mysql_query($insert, $con)) {
$lastid = mysql_insert_id();
echo "Image uploaded. <p /> Your image: <p /> <img src=getpic.php?id=$lastid width='300px' height='300px'>";
} else {
echo "Cannot upload image: ".mysql_error();
}
}
}
?>
</html>
然后 getpic.php 看起来像这样
<?php
mysql_connect("localhost", "username", "password") or die("Cannot connect: ".mysql_error());
mysql_select_db("testpicture") or die("Cannot connect to db: ".mysql_error());
$id = addslashes($_REQUEST['id']);
$image = mysql_query("SELECT * FROM upload WHERE id=$id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];
echo $image;
?>
因此,此代码可以告诉用户上传图像,然后在将其添加到数据库后显示该图像,但是我如何在数据库中显示多张或所有图片。
提前致谢。