Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

MySQL: Image in a blob

by MrStretch (Sexton)
on Mar 28, 2005 at 23:41 UTC ( #442996=perlquestion: print w/replies, xml ) Need Help??

MrStretch has asked for the wisdom of the Perl Monks concerning the following question:

I am playing around with MySQL and my website. Currently I have all my images stored on disk as seperate files, but this is getting cluttery. I want to be able to move the images over to my SQL database, but I am running into a few problems.

I am able to get the image into the database and it all works out nice and dandy; however, I ran into a brick wall when trying to retrieve the image and place it in a html page I keep outputting nasty image code. I have searched the internet for quite some time looking for answers, but it seems I'm the only one stupid enough to have this problem. I know there is some simple solution to this. I just can't seem to figure it out.

I don't necesarily want a flat out answer, a point in the right direction would be a big helper though.

Replies are listed 'Best First'.
Re: MySQL: Image in a blob
by cowboy (Friar) on Mar 29, 2005 at 00:56 UTC
    This is a common problem I've seen made, you are most likely printing the image, into the html page, rather than using an img src tag. Try the following (assuming you're using
    <!-- image.html --> <img src="/cgi-bin/">

    #!/usr/bin/perl # use strict; use warnings; use DBI; use CGI; my $q = CGI->new(); my $dbh = DBI->connect(....); # db setup stuff here my $sth = $dbh->prepare("SELECT image_blob FROM images WHERE image=?") +; my $result = $sth->execute($q->param('image')); if ($result && $result->rows()) { my $image_blob = $result->fetchrow_array(); print $q->header( -type => 'image/jpeg' ); print $image_blob; exit; }
    Note, this code is untested, but I do something similar with images, using Mason under mod_perl
Re: MySQL: Image in a blob
by jZed (Prior) on Mar 28, 2005 at 23:49 UTC
    If you are on windows, did you binmode STDOUT before printing the blob? Are you send the right content-type header along with the image?
Re: MySQL: Image in a blob
by esskar (Deacon) on Mar 28, 2005 at 23:50 UTC
    well, i can not really tell what your problem is, since you haven't showed any code but it i assume that you have not set bimode on STDOUT before printing the blob?!?
      Well, it turns out that i did not binmode STDOUT. I can't believe that was the problem. Thanks for the help.
Re: MySQL: Image in a blob
by Anonymous Monk on Mar 29, 2005 at 00:54 UTC

    Are you trying to link to the images or load them into the page inline?

    if you are trying to use a CGI script to retrieve the images, for IE you need to fake it out with a fake param at the end, so that your URL ends in .jpg (or whatever)

    you also need to be aware of content-type, and issues regarding extraneous whitespace.

    Have fun.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://442996]
Approved by moot
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2023-03-27 20:31 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (65 votes). Check out past polls.