Page 1 of 1

Display Image stored on MySql ??

Posted: Sat Aug 06, 2005 10:31 am
by dynamic_desi
Hi All,

I have few images stored on MySql server as type blob.

while i try and display the image using echo in a php script, it show junk on the page??

Any suggestions on how i can display an image stored on the database server onto the webpage. I am trying to make a picture gallery.

thanks for your help in advance

dynamic_desi :)

Posted: Sat Aug 06, 2005 10:41 am
by feyd
you will need a script that can identify and extract out the image. Basically, your page needs to use this script as its image links with some identifier that you can grab the image through.. the script will need to use header() to set the mime-type correctly (and remember to use header() to pass back the size of the image being sent in "content-length")

Works fine as a seprate script, but not as a module....

Posted: Sat Aug 06, 2005 1:53 pm
by dynamic_desi
Hi,

After trying the header thing which looks like this

Code: Select all

<?php


if(isset($_GET['id']))
{
$id=$_GET['id'];
$connection = mysql_connect('localhost', 'root', 'passwordl');
$dbselect   = mysql_select_db('images');

$query = "select bin_data, filetype from binary_data where id=$id";
$result = mysql_query($query);

$data = mysql_result($result, 0, "bin_data");
$type = mysql_result($result, 0, "filetype");

header("content-type: $type");
echo $data;
}
?>

However if i use the same as a module in this script....it displays junk... :cry:

Code: Select all

function showItems($subcatid)
{
if (!$subcatid || $subcatid=='')
     return false;


$query = "select * from items where subcatid = '$subcatid'";
$result  = mysql_query($query) or die('Error, query failed');
//$row = mysql_fetch_array($result);
$connection = mysql_connect('localhost', 'root', 'password');
$dbselect   = mysql_select_db('images');

$query = "select bin_data, filetype from binary_data where id=8";
$result = mysql_query($query);

$data = mysql_result($result, 0, "bin_data");
$type = mysql_result($result, 0, "filetype");

//header("content-type: $type");
//echo $data;

?>
<table  border="0" cellspacing="1" cellpadding="1">
<?
while ($row = mysql_fetch_array($result))
{
extract($row)
?>
<tr>
<td width="100" height="120" align="center"><?php header("content-type: $type"); echo $data; 
?>
<br><?php echo $row[1]; ?></td>
<td width="430"><?php echo $row[4]; ?><br>$<?php echo $row[2]; ?></td>
</tr>
<?
}
?>
</table>
<?
}
I wonder what am i doing wrong.....any help will be a great favor....

thanks

dynamic desi

feyd | I do not like getting in the habit of fixing people's posts..

Posted: Sat Aug 06, 2005 3:56 pm
by s.dot
while I do not know the solution I do know that it would be much easier for people to figure out if you used the [ php ] and [ /php ] tags :-D

Posted: Sat Aug 06, 2005 5:01 pm
by feyd
you cannot output images at the same time as HTML... choose one.

Images are sent in web pages via a secondary stream

Posted: Sat Aug 06, 2005 6:10 pm
by s.dot
feyd | I do not like getting in the habit of fixing people's posts..
I happen to think you love it. Perhaps I should randomly do it to keep you occupied. :oops:

Posted: Sat Aug 06, 2005 7:03 pm
by feyd
I happen to think you love it.
I very much don't. Starting soon, I'll put a limit as to how many times I'll fix someone's posts, after-which they will not recieve my help until such time as they start doing them correctly.

What if i try this???

Posted: Sun Aug 07, 2005 10:38 am
by dynamic_desi
Hi,

I think it will be a mess if i try and display binary data to HTML webpage. Now instead of uploading the image to mysql, i am trying to upload it to a folder and i save the path to that uploaded file on the server.

The upload part works fine, plus the query saves the path to the server too. But it has some troubles retreving the image. Seems like i am not saving the path in a right format on the mysql server. I've defined the pathname coloumn as"VARCHAR".

Is that correct??? If not what would be the best way to do it??


Code: Select all

$doc_directory = "/var/www/html/";
$my_file ="images/".$fileName;
1
$copy_path = $doc_directory.$my_file;

if(!copy($tempName, $copy_path))
{
echo "file upload failed";
}

else{ echo"file uploaded at $fileName";}

$query = "insert into product (itemName, itemPrice, subcatid, imagepath, description)".
"values ('$itemName', '$itemPrice', '$subcatid', '$fileName', '$itemdesc')";
That is my upload query.

And here's my retrive query

Code: Select all

$query = "select * from product where subcatid = '$subcatid'";
$result  = mysql_query($query) or die('Error, query failed');
//$row = mysql_fetch_array($result);

?>
<table  border="0" cellspacing="1" cellpadding="1">
<?
while ($row = mysql_fetch_row($result))
{
extract($row)
echo $row[5];
?>
<tr>
<td width="150" height="120" align="center"><?php echo '<img src="/images/$row[5]" width="150" height="150" >';
Please Help!!

This time i've tried using the PHP tags from the menu, i hope it works...lol

dynamic_desi


feyd | this is the final time I fix your posted code. I'd suggest you study how bbcode tags work..

Posted: Sun Aug 07, 2005 10:55 am
by feyd
varchar is correct.. however, your retrieval seems a bit odd.. Are you sure the image filename is stored in the 6th field returned?

hi

Posted: Sun Aug 07, 2005 11:19 am
by dynamic_desi
Hey,

Yes...the retreival is pretty respective to the way i am storing the path name into the database.

Before displaying the image, if i try and use

echo $row[5] ;

just to debug if it returns the correct path name, the entire script crashes.

its driving me nuts...lol

dynamic_desi

Posted: Sun Aug 07, 2005 2:56 pm
by feyd
your echo in the retrieval is using a single quote string. Single quote strings do not parse any variables stored within them. Also, running extract() on a numeric array will do nothing.