如何显示存储在PHPMyAdmin,Database ..以及其他表列中的多个图像?

时间:2012-12-22 13:54:40

标签: php html database phpmyadmin

我希望在PHP数据库中显示存储在表格中的图像,表格名称为“礼物”,包含图像的列命名为“ pics

(我知道这不是一个好方法,但我需要这样做)

现在当我运行以下代码时:

<!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=iso-8859-1" />

<title>Untitled Document</title>

</head>

<body>

<?php

$con = mysql_connect("localhost","root","");

if (!$con)

 {

  die('Could not connect: ' . mysql_error());

 }


mysql_select_db("sendemotions", $con);


$result = mysql_query("SELECT * FROM gifts");


echo "<table border='1'>

 <tr>

  <th>Item Picture/th>

 <th>Name Of Item</th>

 <th>Item Type</th>

 <th>Item Price</th>


</tr>";


while($row = mysql_fetch_array($result))

  {

 echo "<tr>";


 echo "<td>" . $row['pics'] . "</td>";

 echo "<td>" . $row['g_name'] . "</td>";

   echo "<td>" . $row['g_price'] . "</td>";

  echo "<td>" . $row['g_type'] . "</td>";


  echo "</tr>";

 }

 echo "</table>";


mysql_close($con);

?> 

</body>

</html>

我得到一个显示的表,其名称中的奇怪值,图片: this is what the output is

请告诉我错误是什么以及如何解决这个问题:(我非常需要这样做。

3 个答案:

答案 0 :(得分:2)

我认为你需要第二个PHP脚本来获取图像。然后,此脚本需要发送图像标题以显示图像。 考试将是这样的:

<?php

$id = $_GET['id']; #WARNING INSECURE. FOR DEMO ONLY!!!

connectToDatabase(); #However you do it

$sql = "SELECT pics FROM gifts WHERE ID = $id";

$result = mysql_query($sql) or die(mysql_error());  
$row = mysql_fetch_array($result);

header("Content-type: image/jpeg");
echo $row['pics'];
$db->close();

?>

警告:Abovo代码非常不安全!你需要检查$ _GET ['id']是否为整数!

在原始的PHP脚本中,您可以将其包含在imagerow中。

echo '<td><img src="http://yourdomain/imagescript.php?id="' . $row['id'] . '"></td>';

此代码不是testet ...只是一个快速概述。

答案 1 :(得分:2)

首先,您必须学习HTML基础知识 在HTML中,图像通过<img>标记显示,给定URL,指向资源。

接下来,不是将图像存储在数据库中,而是将文件放在目录中,这对文件来说非常不自然。如果你在id之后重命名它们,你甚至不需要将路径存储在数据库中 - 只需在飞行中构建它:

<img src="/img/gifts/<?=$row['id']?>.jpg">

答案 2 :(得分:1)

我认为您将图像作为Blob存储在数据库中。然后你必须在php输出中设置标题内容类型 请检查此链接show image from blob mysql

另一种方法是使用base64_encode
base64_encode
displaying an image stored in a mysql blob