php - 使用 PHP 从数据库中获取图像

标签 php mysql image

我基本上试图创建一个购买页面,允许用户浏览存储在数据库中的图像文件集合并选择一个或多个图像进行购买。我已经能够将图像上传到数据库中,但不知道如何获取它们并将其显示在购买页面上。 我的 table 看起来像这样

Column               Type          Null       Default

ID                   int(11)       No   
ArtName              tinytext      No   
ArtDescription       text          No   
ArtistName           tinytext      No   
ArtistDescription    text          No   
Price                bigint(11)    No   
FileName             varchar(44)   No   
ImgFile              longblob      No   

到目前为止我的购买页面: 购买页面当前名为catalog.php

<?php
error_reporting(0);
$_SESSION['currentpage']="catalog.php" ;
$artist = $_REQUEST['artist'] ;
$con=mysqli_connect('localhost','SouravBasuRoy','2525','MyteraArt');
$result = mysqli_query($con,"SELECT * FROM `paintings` WHERE `ArtistName` = '$artist'");
while($row = mysqli_fetch_array($result))
{
echo "<p>Painting Name : " . $row['ArtName'] . "<br>" . "Artist Name : "  .  $row['ArtistName'] . "<br>" .  "Price : " . $row["Price"] . "</p><br>" ;
}
?>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Catalog | Mytera Art</title>
<link href="styles.css" rel="stylesheet" type="text/css">
</head>
<body>
<section class="login">
<?php
$username = $_COOKIE['username'] ;
if ($username =="")
{
echo '<form name="login" action="login.php" method="post">
<label>Login</label>
<label for="Username">Username :<input type="text" name="username"></label>
<label for="Password">Password :<input type="password" name="password"></label>
<input type="submit">
</form>' ;
}
else
{
echo '<form action="logout.php">Logged in as : ' . $_COOKIE["username"] . ' ' . '<input  type="submit" value="Logout"></form>' ;
}
?>
</section>
<header>
<h1>Mytera Art</h1>
</header>
<nav>
<ul>
<li><a href="loggedin.php">Home</a></li>
<li><a href="catalog.php">Catalog</a></li>
<li><a href="purchase.php">Purchase</a></li>
<li><a href="aboutus.php">About Us</a></li>
<li><a href="contactus.php">Contact Us</a></li>
</ul>
</nav>
<aside>
<ol>
<li><a href="catalog.php?artist=Amedeo Modigliani">Amedeo Modigliani</a></li>
<li><a href="catalog.php?artist=Claude Monet">Claude Monet</li>
<li><a href="catalog.php?artist=Edvard Munch">Edvard Munch</li>
<li><a href="catalog.php?artist=Gustav Klimt">Gustav Klimt</li>
<li><a href="catalog.php?artist=Hans Holbein">Hans Holbein</li>
<li><a href="catalog.php?artist=Kazimir Malevich">Kazimir Malevich</li>
<li><a href="catalog.php?artist=Pablo Picasso">Pablo Picasso</li>
<li><a href="catalog.php?artist=Paul Cézanne">Paul Cézanne</li>
<li><a href="catalog.php?artist=Peter Paul Rubens">Peter Paul Rubens</li>
<li><a href="catalog.php?artist=Pierre-Auguste Renoir">Pierre-Auguste Renoir</li>
<li><a href="catalog.php?artist=Pontormo">Pontormo</li>
<li><a href="catalog.php?artist=Thomas Eakins">Thomas Eakins</li>
<li><a href="catalog.php?artist=Titian">Titian</li>
<li><a href="catalog.php?artist=Vincent van Gogh">Vincent van Gogh</li>
<li><a href="catalog.php?artist=Wang Meng">Wang Meng</li>
</ol>
</aside>
<footer>
</footer>
</body>
</html>

我需要根据艺术家姓名获取图像并将其显示在屏幕上的代码。 一位特定的艺术家可能拥有不止一幅图像。 图像存储在 www/images 文件夹中,因此如果有一种方法我可以只使用图像的文件名而不实际获取文件本身,我认为会更容易,并且我会这样做。但无论你建议什么。这是一个大学项目。 谢谢。

最佳答案

我已经在评论中指出,这个问题不是很具体,尽管您可以查看以下手册和文档以获取更多信息,以便做到这一点,您想要存档的内容。

http://www.php.net/manual/en/book.mysql.php

http://dev.mysql.com/doc/refman/5.1/en/

要从数据库中提取图像和图片,您需要使用 SELECT 查询。获取它们不应该成为问题,因为如果您精确查询所需的信息,几乎没有任何内容可获取。但是,您可能需要循环遍历结果。

要查询表中的图像,您的查询可能如下所示:

SELECT ImgFile FROM yourtable WHERE ArtName = 'yourname'

您还应该考虑使用mysqli因为 mysql_* 现已弃用。

关于php - 使用 PHP 从数据库中获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23095847/

相关文章:

java - 图像不会出现在 JLabel 中

php - 如何检查相似的字符串

javascript - 如何使用 jquery 将关注按钮从关注更改为关注

php - 从 windows azure 中的 codeigniter 中的 URL 中删除 index.php

php - 如何为模型的所有搜索操作设置默认条件?

javascript - 迷你图像浏览器(如 myfonts.com)

mysql - 在 MySql 中查找 n 天后是否有周年纪念日

javascript - 使用 Javascript 在选择框更改时显示 MySQL 列数据

c# - 将字符串转换为连接到mysql的gridview中的日期

PHP/GD,如何将圆圈从一个图像复制到另一个图像?