Teddy,
Yes, you're right. I changed this line:
   while ((my $ref) = $sth2 ->fetchrow_hashref("$num")) {...
by removing the parameter, and quotes around 'my $ref' :
   while (my $ref = $sth2 ->fetchrow_hashref()) {...
and the script works now.

Thanks for your help.
John


Octavian Rasnita wrote:

Hi,

As far as I know, the method fetchrow_hashref doesn't require a parameter
(like fetchall_hashref).

Teddy

----- Original Message ----- From: "john" <[EMAIL PROTECTED]>
To: <beginners@perl.org>
Sent: Tuesday, December 13, 2005 09:30 AM
Subject: DBI problem


Hi all-
I'm attempting to loop thru an input file,  executing a SELECT query
with the value from each line.
The script works for the first iteration, and then gives these error
messages:

DBD::mysql::st fetchrow_hashref failed: fetch() without execute() at
./describe_skus.pl line 27, <INFILE> line 2190.
Use of uninitialized value in print at ./describe_skus.pl line 27,
<INFILE> line 2190.
Use of uninitialized value in print at ./describe_skus.pl line 27,
<INFILE> line 2190.
Use of uninitialized value in print at ./describe_skus.pl line 27,
<INFILE> line 2190.
Use of uninitialized value in print at ./describe_skus.pl line 27,
<INFILE> line 2190.

(INFILE line 2190 is the last line.)
Here's my script:

#!/usr/bin/perl -w
use strict;
use DBI;

# purpose: To take sku numbers from a text file, "no-imgs.txt",
# and print out their database descriptions, to a separate text file,
"need-images.txt".

open INFILE, "no-imgs.txt" or die "Input file failed to open: $!\n";
open OUTFILE, "> need-images.txt" or die "Output filehandle not open:
$!\n";
my @nums = <INFILE>;

my $dbh=DBI->connect('dbi:mysql:productdb','script','') ||
  die "Error connecting to database: $DBI::errstr\n";

my $query2 = qq{SELECT `sku`, `title`, `vendor_stock_no`, `vendor_id`
FROM `products` WHERE `sku`=?};

for my $num (@nums) {
   last unless $num; #exit at end-of-input-file
   next if $num =~ /^\s*\t*\n$/;  # to skip over blank lines
   $num =~ s/^\s+//; # delete leading spaces
   chomp $num;

   my $sth2= $dbh->prepare($query2) || die "Prepare failed:
$DBI::errstr\n";
   $sth2->execute ("$num") || die "Couldn't execute query:
$DBI::errstr\n";
   while ((my $ref) = $sth2 ->fetchrow_hashref("$num")) {
       print OUTFILE "sku, \t title, \t vendor_stock_no, \t vendor_id
\n",
            $ref->{sku}, $ref->{title}, $ref->{vendor_stock_no},
$ref->{vendor_id};
   }
   $sth2->finish ();
}

$dbh->disconnect || die "Failed to disconnect\n";
close OUTFILE or die "cannot close output file need-imgs.txt: $!";
#End

Thanks in advance for your suggestions.
John



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to