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

Hello Monks,

I have two images in a database that I would like to display on a web page.

When I run my script, the column that should have the image displays a "1". I do not understand why that is happening.

The first batch of code is a script that should be the link for the image. This has worked in a prior script for me.

#!/usr/bin/perl -Tw # serve_image.cgi - serve an image file as a Web page use CGI qw(:standard escape escapeHTML); use DBI; use strict; use CGI::Carp qw(fatalsToBrowser); use lib qw(/usr50/home/summitwe/public_html/library); use WebDB; #@ DISPATCH if (defined (param ("item_id"))) { display_image (param ("item_id"), param ("thumbnail")); } elsif (defined (param ("gallery"))) { display_gallery () } else { error ("Unknown request type"); } #@ DISPATCH exit (0); #@ DISPLAY_IMAGE sub display_image { my ($item_id, $show_thumbnail) = @_; my $col_name = (defined ($show_thumbnail) ? "thumbnail" : "picture"); my ($dbh, $mime_type, $data); $dbh = WebDB::connect (); ($mime_type, $data) = $dbh->selectrow_array ( "SELECT mime_type, $col_name FROM catalog_pet WHER +E item_id = ?", undef, $item_id); $dbh->disconnect (); # did we find a record? error ("Cannot find image named $item_id") unless defined ($mime_t +ype); print header (-type => $mime_type, -Content_Length => length ($dat +a)), $data; } #@ DISPLAY_IMAGE # Present gallery of names and images in the image table. Present the # thumbnail version of each image, but embed the image inside a hyperl +ink # that selects the full size image for display. #@ DISPLAY_GALLERY sub display_gallery { my ($dbh, $sth); print header (), start_html ("Image Gallery"); $dbh = WebDB::connect (); $sth = $dbh->prepare ("SELECT item_id FROM catalog_pet ORDER BY it +em_id"); $sth->execute (); # we're fetching a single value (name), so we can call fetchrow_ar +ray() # in a scalar context to get the value while (my $item_id = $sth->fetchrow_array ()) { # encode the name with escape() for the URL, with escapeHTML() + otherwise my $url = url () . sprintf ("?item_id=%s", escape ($item_id)); $item_id = escapeHTML ($item_id); print p ($item_id), a ({-href => $url}, # link for full size image # embed thumbnail as the link content to make it click +able img ({-src => "$url;thumbnail", -alt => $item_id}) ), "\n"; } $sth->finish (); $dbh->disconnect (); print end_html (); } #@ DISPLAY_GALLERY #@ ERROR sub error { my $msg = shift; print header (), start_html ("Error"), p (escapeHTML ($msg)), end_html (); exit (0); } #@ ERROR
The second batch of code is where I'm calling the link to display the image:
sub get_product_table { my $sth = shift; my @row; while (my $ref = $sth->fetchrow_hashref ()) { my $serve_url = sprintf ("serve_image.cgi?item_id=%s;thumbnail", +escape ($ref->{item_id})); # generate a form allowing a quantity of the item to be added # to the cart push (@row, start_form(-method=>'GET', -action=>url()), hidden( -name => "choice", -override => 1, -default => "add" ), hidden( -name => "item_id", -override => 1, -default => escapeHTML( $ref->{item_id} )), Tr ( td (print img ({-src => $serve_url, -alt => escapeHTML($ref->{item_ +id})})),

I want to display the thumbnail version of the image. As I mentioned, when I run my script, the column that should print the image, prints a "1" instead.

I can really use some help. I've been at this for a while now, and I'm not sure what to do.

Thanks.

update (broquaint): added <readmore> and formatting

Replies are listed 'Best First'.
Re: Displaying Images using Perl
by poj (Abbot) on Jan 28, 2003 at 20:45 UTC
    Last line
    td (print img ({-src => $serve_url, -alt => escapeHTML($ref->{item_ +id})})),
    take the 'print' out
    poj
      Thank you, thank you, thank you.....Now, I can get rid of this headache. :)
Re: Displaying Images using Perl
by steves (Curate) on Jan 28, 2003 at 20:23 UTC

    The easiest way, IMO, to debug something like this is to work backwards. View the page source in your browser and see what that image tag looks like for starters.

      Hello, I looked at the source in my browser and the image tag doesn't appear. I have a column labeled "Image" which a value of 1 in that column. Therefore, someplace in the code I placed in this node is not showing the tag. Any ideas? Thanks
Re: Displaying Images using Perl
by LAI (Hermit) on Jan 28, 2003 at 20:51 UTC

    The following lines kinda jumped out at me, from about the middle of the first batch of code:

    # we're fetching a single value (name), so we can call fetchrow_ar +ray() # in a scalar context to get the value while (my $item_id = $sth->fetchrow_array ())

    Try changing the third line of that to:

    while (my ($item_id) = $sth->fetchrow_array ())

    I'm not sure if DBI's fetchrow_array() uses wantarray or not, but it looks like $item_id is getting set to scalar $sth->fetchrow_array (), which is not what you want.

    So change that line, and let me know if it worked.

    Update: Just saw poj's post. s?he's right. Ignore my suggestion.


    LAI
    :eof
Re: Displaying Images using Perl
by Gilimanjaro (Hermit) on Jan 28, 2003 at 20:57 UTC
    Your booboo is in the line:

    while (my $item_id = $sth->fetchrow_array ())

    fetchrow_array returns an array, which you are assigning to a scalar. This forces the array into scalar context, which results in the number of items in the array. Which is one, because you did a single column query.

    The easiest way to fix it would be to do:

    while (my ($item_id) = $sth->fetchrow_array ())

    Now you're assigning an array to a list, which sequentially fills the elements of the list with the elements of the array... As it's only a single-item list and a single-item array, you get the actual column content... Which is what you wanted all along...

    Happy Coding!

    Update: Nope. I'm wrong... Thankfully I wasn't the only one... :) Sorry...