Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl - Display Image from Database

Status
Not open for further replies.

Speckly

Technical User
Jun 9, 2004
18
GB
Hello,

I am a newbie on Perl. I am creating a site for our bookshop where the details (ISBN, Title, Cost, Book cover Image) of the books that we are selling can be viewed online.

The book records are held in an Access Database. I have managed to display the records for all text fields, but the 'Image' field (OLE Object - a jpeg image of the front cover embedded in the db) I can't get to display. Can you help me??

I have read many help pages on the web and some say to code the following line and then output the image i.e.

Code:
print "Content-type: image/jpeg\n\n";
print $Data{'Image'};

But when I do this all I get is the words "Content-type: image/jpeg" displayed on the screen.

My script is below - can anybody help???

Code:
<%@ Language=PerlScript %>  
<% 
use strict;
use Win32::ODBC;
use vars qw($Request $Response $Application $Session);
use lib $Request->ServerVariables("APPL_PHYSICAL_PATH")->Item . "\\dandy";
use FreezeThaw qw(freeze thaw);
use CGI ":standard";
%>

<!--#include virtual="include/PerlAsp.inc"-->
<!--#include virtual="include/PerlAspTable.inc"-->
<!--#include file="Bookshop.inc"-->

<%
my $db = DandyDb(); #open database connection...
    
if (not ref $db)  
    {
        # if no database - show the error message
        print(h1("Error"), p(strong("Database not    
        available"), " - ", $db), "\n");
        $Response->flush();
    }
    
else  # Get records from Bookshop Table and display in a
      # table on the webpage
    {
    my $Bkshop = DandyTable("Bookshop");     
    my $data; 
    my $sql = "SELECT $Bkshop.ISBN, Title, Cost, Image FROM
    $Bkshop;
      
    if ($db->Sql($sql)) 
    	{
        print p(strong("Database Error:"),br,scalar
        ($db->Error)); 
        $Response->flush();
   	}
   
    else 
        {	
        %>

        <table>
            <tr>
       	         <td>ISBN</td>
       	         <td>Title</td>
       	         <td>Price</td>
       	         <td>Image</td>
            </tr>  
        </table>
      
        <table> 
            <tr> 
        <%     
	  while ($db -> FetchRow()) 
      	    { 
      	     my %Data = $db->DataHash(); 

             print "<td> $Data{'ISBN'}</td>";
             print "<td> $Data{'Title'}</td>";
             
             #Format Price field to 2d. places
             my $price = $Data{'Cost'};
             $price = sprintf('%0.2f', $price);
        
             print "<td> £$price</td>"; 
             #This is where I want to display the image -  
             #but it doesn't work.
             print "<td> $Data{'Image'} </td></tr>";
	     }  
        print "</table>"; 
      } 
 }		
 
$db->Close() if ref($db);
undef($db);
%>
</body>
</html>

The Image field is not displayed. What am I doing wrong??

Many thanks,

Catherine

 
I don't have an answer to your particular problem, but I think it's not the best design approach in the first place. Access is not a very good database, so putting lots of images in it doesn't seem like a very good idea. Instead, I'd suggest storing the name of the image in access, and having all the files in a directory. They are, after all, presumably a bunch of files that are read only, right? You're not changing the images transactionally in the database, and if you are, you shouldn't be using access, as the thing is a hunk of junk.

I know access has one of the friendliest user-interfaces, that's Microsoft's specialty. But it has one of the least forgiving engines -- if two people, or more, try to simultaneously write to that database on your server, odds are your database will crash, your data will get corrupt, it's not pretty!

For a website, mysql is far better, though it too is a bit of a toy if you just use it naively out of the box. If your database is mostly read, for you to publish a catalog and for people to view, than either access or mysql will do, though mysql is probably 1000 times faster, but in either case, don't put the images in the database unless you have a serious reason to do so.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top