我没有在mySQL中使用BLOB存储图像,而是决定尝试使用引用路径。所以我将图像存储到我的“localhost”文件夹中:
localhost/dvd_artwork/image.jpg
因此在我的数据库中,在“dvdimage_path”列下我有TEXT“dvd_artwork / image.jpg”,其中我的id = 1
然而,我现在似乎无法让这个工作
我的catalog.php:
<img src="getImage.php?id=1" alt="" width="175" height="200" />
我的getimage.php:
<?php
$id = $_GET['id'];
// do some validation here to ensure id is safe
$link = mysql_connect("localhost", "root", "");
mysql_select_db("dvddb");
$sql = "SELECT dvdimage_path FROM dvd WHERE id=$id";
$result = mysql_query("$sql");
$row = mysql_fetch_assoc($result);
mysql_close($link);
echo $row['dvdimage_path'];
?>
有什么不对吗?
答案 0 :(得分:2)
从mysql_query中删除引号
// Change that
$result = mysql_query("$sql");
// To that
$result = mysql_query($sql);
答案 1 :(得分:1)
这没有任何意义 - getImage返回一个字符串,而不是图像......
您可能想要替换getImage href,只需生成如下页面:
<?php
$id = $_GET['id'];
// do some validation here to ensure id is safe
$link = mysql_connect("localhost", "root", "");
mysql_select_db("dvddb");
$sql = "SELECT dvdimage_path FROM dvd WHERE id=$id";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
mysql_close($link);
?>
<img src="<?= $row['dvdimage_path'] ?>" alt="" width="175" height="200" />
...或者你想在路径上使用file_get_contents,然后用适当的标题回显:
<?php
$id = $_GET['id'];
// do some validation here to ensure id is safe
$link = mysql_connect("localhost", "root", "");
mysql_select_db("dvddb");
$sql = "SELECT dvdimage_path FROM dvd WHERE id=$id";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
mysql_close($link);
header('Content-Type: image/jpeg'); // Or png, or whatever...
echo file_get_contents($_SERVER['DOCUMENT_ROOT'].$row['dvdimage_path']); // might need a slash between the two variables here...
?>
如果可能的话,我建议采用第一种方式,因为它具有通过服务器配置允许正常标头使用的好处。