Jump to content


Photo

Displaying image stored in mysql db as blob


  • Please log in to reply
2 replies to this topic

#1 zobie

zobie
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 23 June 2006 - 10:07 PM


Hey guys,

I've been trying to display some images stored in a mysql database. The upload script seems to work fine. My problem arises when i try to display the images.

I use two files, one called view.php and the other view_image.php.

The view.php file includes the following code.

$query = "SELECT mime_type,image FROM testblob2
WHERE id = 1";

//the image field is of type longblob and the mime_type field is of type varchar

$result = @ mysql_query ($query,$connection);

$data = @ mysql_fetch_array($result);

if (!empty($data["image"]))
{
// Output the MIME header
// header("Content-Type: {$data["mime_type"]}");
// Output the image
echo $data["image"];
}

When i pulled this script (view.php) through the browser i received the same output that you get when you open up an image file with a text editor. A bunch of weird symbols.

I then included the following code in the view_image.php file:

$query = "SELECT id,image_name FROM testblob2";

$result = @mysql_query($query,$connection);
if($row = @mysql_fetch_array($result))
{
echo "{$row["image_name"]}";
echo "<img src=\"view.php?file={$row["id"]}\">";
}

When i pull up view_image.php through the web browser i get a broken link where the image is supposed to be. I can't seem to resolve this issue.

I also would like to fully understand the line
echo "<img src=\"view.php?file={$row["id"]}\">";
Where does the word "file" come from since i can't find a line where it gets passed to the script and what is the function of the "?"

I guess its similar to the url above [a href=\"http://www.phpfreaks.com/forums/index.php?act=post&do=new_post&f=1\" target=\"_blank\"]http://www.phpfreaks.com/forums/index.php?...do=new_post&f=1[/a]

why is the word or value "act=post&do=new_post&f=1" used and where does it come from. Does it have to be passed to the script using GET or POST .

I'm just trying to pinpoint where i've gone wrong. I'm a relative newbie to php coding but i don't believe that retrieving and displaying an image from a database should be so frustrating. Must be something simple that i'm missing.

Another forum that i checked mentioned that a broken link for the image usually means that the header has not be sent. Should I be sending the filesize with the header as well .

I'm not debating whether or not I should have stored the absolute paths instead of storing the images in the databse as blobs. I'd just like to deal with the issue at hand. Displaying the images that are currently stored.


#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 24 June 2006 - 06:49 AM

index.php?file=123

this passes info to index via the GET method. You can access it like so:

echo $_GET['file']; //prints out 123



Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 zobie

zobie
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 26 June 2006 - 03:10 PM

[!--quoteo(post=387388:date=Jun 24 2006, 02:49 AM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ Jun 24 2006, 02:49 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
index.php?file=123

this passes info to index via the GET method. You can access it like so:

echo $_GET['file']; //prints out 123
[/quote]


Thanks Crayon Violent,

I'm sorry that I didn't respond sooner. I had to head out for the weekend. Got back in last night. Thanks for the info. I understand that aspect now. Do you have any ideas as to how to display the photos from the mysql database though?

I'd greatly appreciate a response


zobie




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users