显示BLOB文件
这是我的PHP脚本,显示广播电台的时间表:显示BLOB文件
<div class="divider"></div>
<div class="main" style="width:552px;">
<img src="$image" width=115 height=60>
<div class="time">$airtime</div>
<div class="show"><h3><a href="$link><b>$presenter</b></a></h3>
<p>$showdesc</p></div>
<div class="footer"></div>
</div>
</div>
<div class="footer"></div>
<div class="bottom"></div>
</div>
与美元符号的值代表我的数据库,这是radiopresenters字段名称。
我如何获得这个作为PHP脚本工作,并显示数据库中的值? 除了存储为BLOB格式的图片字段以外,字段中的所有值均以TEXT格式存储。
通话时间存储在一个单独的数据库中,名为radioschedule它具有所有4个字段ib,并且我打算通过一些关系手段将它们连接在一起。
让上面显示的最好方法是什么,特别是BLOB部分?
你问如何将这些值放入你的文件?
至于文字的人,你可以只使用
<?=$airtime?>
,或者如果你的服务器安装程序不支持短标签只使用
<?php echo $airtime?>
我只想做这个在线与你的PHP。
例子:
<div class="time"><?=$airtime?></div>
对于BLOB,我会建议不这样做,只是存储在服务器上的图像,并在数据库中存储的图像文件的名称。但如果你打算这样做,我认为你可以做到这一点的唯一方法是通过一个单独的脚本返回一个图像文件。事情是这样的:
<?php
// Do all your db stuff here, grab the blob file. Probably from a $_GET paramater
$image = $row['image'];
header("Content-type: image/jpeg"); // or gif, etc...
echo $image;
die();
?>
然后在你的其他的文件,你会做这样的事情:
<img src="imagescript.php?person_id=<?=$person_id?>" width=115 height=60>
你想要做的是建立一个show_image.php
脚本什么,例
show_image.php
<?php
$id = (isset($_GET['blid']) && is_numeric($_GET['blid'])) (int)$_GET['blid'] : false;
if($id)
{
if(false !==($res = mysql_query('SELECT blob_data FROM table WHERE blob_id = ' . $id))
{
$data = mysql_fetch_assoc($res);
header("Content-type: image/jpg"); //Send the content Type here.
print $data['blob_data'];
exit;
}
}
?>
然后在你的html文件中,你会做下面的事情。
<div class="divider"></div>
<div class="main" style="width:552px;">
<img src="show_image.php?id=<?php echo (int)$id?>" width=115 height=60>
<div class="time">$airtime</div>
<div class="show">
<h3><a href="$link><b>$presenter</b></a></h3>
<p>$showdesc</p></div>
<div class="footer"></div>
</div>
</div>
<div class="footer"></div>
<div class="bottom"></div>
这大致如何做,另一个指针是当你通过你的主页中选择你的数据,你不应该选择BLOB数据,因为它会减慢你的应用程序下来,show_image.php可能需要更多的工作,如其举例仅用于
和平。
您可以将存储为blob的图像写入数据库到文件中。然后,您可以使用网址作为图像源。
假设$presenter
没有任何文件/ URL在它保留字符,并且$image
存储为一个精确的二进制JPG(或PNG或GIF等),你可以这样做:
<?php
if($fh = fopen("/webroot/images/{$presenter}.jpg", "wb")) {
fwrite($fh, $image) ;
fclose($fh) ;
}
?>
<img src="/images/<? echo $presenter ; ?>.jpg" width="115" height="60">
我会建议你制定一些缓存文件的方法,所以它不必为每个页面加载写出来。
这是不正确的缓存时,它每次运行,什么是说图像文件存在,你的代码有点容易出错。 – RobertPitt 2010-09-14 22:25:41
的确 - 为什么我建议在我的答案中添加缓存。 – Gus 2010-09-14 22:38:01
我在问如何创建一个将用于包含文件的脚本,并且如果在我的数据库中没有选择任何内容,则使用ifelse。将这两个表连接在一起是一个问题 - 它会导致关系错误,这是由我在做关系数据库之前得到的散列符号标记的? – whitstone86 2010-09-15 12:03:22