...

View Full Version : selecting images from mysql db



Kal
10-30-2006, 02:24 PM
Hi

i have a script which will upload an image into my mysql database and store it in the BLOB type, however i am having trouble trying to display this image when i call using a query.

any help would be great.


<?php

$id=$_GET['id'];

require('database.php');

$query = "select
id,
title,
description,
DATE_FORMAT(date_posted,'%d/%m/%Y') as date_posted,
image
from orb_orb.press_release
where id = '$id'";

$result = mysql_query ($query) or die (mysql_error());

if (mysql_num_rows($result)>0)

{

while($row=mysql_fetch_array($result))

{

?>

<table border="0" width="100%" cellpadding="0" cellspacing="0">

<tr>
<td><strong><?php print $row['title'];?></strong></td>
</tr>

<tr>
<td>&nbsp</td>
</tr>

<table border="0" cellpadding="0" cellspacing="0" align="right">

<tr>
<td>&nbsp</td>
<td valign="top"><img src="/Press Release Layout/Press_Images/<?php print $row['image']; ?>" align="right"></td>
</tr>

</table>

<p><?php print nl2br ($row['description']);?></p>

<tr>
<td>&nbsp</td>
</tr>

<tr>
<td><p><b>Date Posted:&nbsp&nbsp&nbsp</b><?php print nl2br ($row['date_posted']);}}?></p></td>
</tr>

<tr>
<td>&nbsp
</tr>

</table>

firepages
10-31-2006, 01:50 AM
This is one of the reasons storing images in a DB is not always a great idea (but thats an argument for another day;))

you can't output html and image data at the same time like that , you would have to have a separate script for displaying the image and then link to that as per usual ...



<?php /*display.php, display an image from database*/
$query = "select image from orb_orb.press_release where id = '{$_GET['id']}'";
$result = mysql_query ($query) or die (mysql_error());

/*assuming a jpg !!*/
header("content-type: image/jpg");
echo $result['image'];
?>

then in your main script ...


<?
<img src="display.php?id=<?=$id;?>" />
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum