★ wanayoo — archive 1999 http://www.devshed.com/Talk/Forums/Forum4/HTML/000384.htmlNouvelle recherche | Portail wanayoo
DevShed Menu
* DevShed Home
* Developer News
* DevShed Brain Dump




UBBFriend: Email This Page to Someone!
  DevShed Discussion Forums
  MySQL
  images in mysql through perl dbi interface

Post New Topic  Post A Reply
profile | register | preferences | faq | search

next newest topic | next oldest topic
Author Topic:   images in mysql through perl dbi interface
dis
Junior Member
posted April 23, 2000 08:13 PM     Click Here to See the Profile for dis     Edit/Delete Message Reply w/Quote
Does any know where I can get an image
into a blob type column from a perl dbi script?
tried the following substitutions with no success.

$var=~s/\'/\\\'/g;
$var=~s/\\/\\\\/g;
$var is where the image is located the
insert always barfs
any suggestions how to get it in?

Thanks in advance,

Rick
rick@erealestateforyou.com

sauli
Junior Member
posted July 23, 2000 01:49 PM     Click Here to See the Profile for sauli   Click Here to Email sauli     Edit/Delete Message Reply w/Quote
[QUOTE]Originally posted by dis:
[B]Does any know where I can get an image
into a blob type column from a perl dbi script?
tried the following substitutions with no success.

$var=~s/\'/\\\'/g;
$var=~s/\\/\\\\/g;
$var is where the image is located the
insert always barfs
any suggestions how to get it in?

Thanks in advance,

Rick
rick@erealestateforyou.com


Here is my way to solve this problem, by using an filehandler $ICONFILE

my $icon;
my $filesize;
if ($ICONFILE ne '') {
$filesize = -s $ICONFILE;
my $in = read($ICONFILE, $icon, $filesize,0);
if ($in != $filesize) {
error("Error reading the file $ICONFILE");
}

$icon =~ s/\\/\\\\/g; # first do this
$icon =~ s/\000/\\0/g; # change NUL
$icon =~ s/\'/\\\'/g;
$icon =~ s/\"/\\\"/g;
}

Now you can use $icon within the sql-statement like:
$sql = qq{INSERT INTO images VALUES ("$id", "$mimetype","$icon")};

To get the image out of the database i wrote this perl-script:


#!/usr/bin/perl -w

use strict;
use DBI;
use CGI qw/:standard/;
require "database.sub";
# in database.sub there are the functions to connect (Connact_DB), run sql-statements (Do_SQL) and to disconnect the database.

my $user = 'test';
my $passwd ='none';
my $id = param('id');

&Connect_DB($user,$passwd);

my $sql = "SELECT icon,mimetype FROM artikel WHERE id = $id";
my $sth = &Do_SQL($sql);
my $file = $sth->fetchrow_hashref;
my $icon = $file->{'icon'};
my $mimetype = $file->{'mimetype'};
$sth->finish;
&Disconnect;

print header('$mimetype');
print $icon;

exit(0);
#end


I'll hope it will help you.


Sauli

All times are MST (US)

next newest topic | next oldest topic

Administrative Options: Close Topic | Archive/Move | Delete Topic
Post New Topic  Post A Reply
Hop to:

Contact Us | DevShed.com

Copyright © 1997-2000 ngenuity. All rights reserved.

Powered by: Ultimate Bulletin Board, Version 5.41a
© Infopop Corporation (formerly Madrona Park, Inc.), 1998 - 1999.