如何显示我的数据库中的图像(BLOB类型数据)?

时间:2014-08-18 08:46:06

标签: php mysql

这是上传表格

CREATE TABLE `upload` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `deskripsi` text,
  `filetype` varchar(200) default NULL,
  `filedata` longblob,
  `filename` varchar(200) default NULL,
  `filesize` bigint(20) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=49 ;

这是文章

的表格
CREATE TABLE `info` (
  `id_info` int(10) NOT NULL auto_increment,
  `judul_info` varchar(50) collate latin1_general_ci NOT NULL,
  `konten` varchar(50000) collate latin1_general_ci NOT NULL,
  `diubah_oleh` varchar(20) collate latin1_general_ci NOT NULL,
  `id_kategori` int(10) NOT NULL,
  `tgl_buat` timestamp NOT NULL default '0000-00-00 00:00:00',
  `tgl_ubah` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  `dibuat_oleh` varchar(20) collate latin1_general_ci NOT NULL,
  `id` int(10) NOT NULL,
  PRIMARY KEY  (`id_info`),
  KEY `id_kategori` (`id_kategori`),
  KEY `id` (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=5 ;

这是我的index.php

的源代码
<?php
include "config.php";
$query= "SELECT upload.filetype, upload.filename, upload.filedata,
info.judul_info, info.konten, info.diubah_oleh, info.id_info, info.tgl_buat,
info.tgl_ubah, info.dibuat_oleh, info.id 
FROM info 
JOIN 
upload 
ON info.id = upload.id WHERE id_info='4'";
$runquery = mysql_query($query);

while($result = mysql_fetch_array($runquery))
{
$id = $result['id'];
echo "<img src=get_image.php?id=$id>";
?>

<div class="caption">
<?php
$id_info = $result['id_info'];
$judul = $result['judul_info'];
$konten = $result['konten'];
echo "<h3>$judul</h3>";
echo "<p>".substr($result['konten'], 0, 100)."...</p>";
echo "<a href=detail_info.php?page_detil=$id_info>Selengkapnya</a><br><br>";
}
?>

而且,这是我的get_image.php源代码

<?php
include "config.php":

$id = addslashes($_REQUEST['id']);

$image = mysql_query("SELECT * FROM upload WHERE id=$id");
$image = mysql_fetch_assoc($image);
$image = $image['filedata'];

header("Content-type: image/jpeg");
echo $image;
?>

我希望图像与文章一起显示,我可以显示文章的标题和内容,但图像不会显示出来。有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

首先:请避免直接将用户输入传递给查询!您还应切换到mysqli或PDO:http://code.tutsplus.com/tutorials/pdo-vs-mysqli-which-should-you-use--net-24059

要解决您的问题,您应引用图片href:

echo "<img src='get_image.php?id=$id'>";

如果这没有帮助,请在浏览器中打开get_image.php。图像是否显示?如果没有,请检查查询的结果(只需注释标题行以查看输出)。