Re: Why is $sth->rows() uninitialized?
Greg Meckes <[email protected]>
| Newsgroups | gmane.comp.db.mysql.perl |
|---|---|
| Message-ID | <[email protected]> |
It's possible that the value of literal zero being returned is being interpreted as uninitialized
by Perl.
You should try something like this:
if (! defined($sth->rows()) || $sth->rows() == 0)) {
print STDERR "Duplicate item found at line $ln: $_\n";
}
--- KEVIN ZEMBOWER <[email protected]> wrote:
> Thanks to the suggestions of Greg and others, I was able to use $sth-rows() to determine if a
> record was present or not in my table. Thanks, Greg. The section of code I used successfully is:
> $kwsth->execute($_) or warn "Problem with execute: $DBI::errstr\n";
>
> if ($kwsth->rows() == 0) {
> print "Keyword $_ not found " if $debug;
> $inskwsth->execute($_);
> $keywordid = $dbh->{'mysql_insertid'};
> print "NEW id = $keywordid\n" if $debug;
>
> } else { # If the keyword doesn't already exist in the table, else...
>
> $keywordid = $kwsth->fetchrow_array or warn "Problem with fetch: $DBI::errstr\n";
> print "Keyword ID: $keywordid\n" if $debug;
> } # else if the keyword DOES exist in the table already
>
>
> Now I'm trying to apply this to another program in an almost identical fashion, and don't
> understand why I'm getting an error. The section of code is:
> my $sth = $dbh->prepare("SELECT baseitemid FROM langversions WHERE baseitemid =? AND
> langid=?");
> $sth->execute($baseitemid, $langid);
>
> if ($sth->rows() == 0) { #THIS is line 70
> print STDERR "Duplicate item found at line $ln: $_\n";
> } else {
> my $sth = $dbh->prepare("INSERT INTO langversions (baseitemid, langid, title, cost,
> available) VALUES (?, ?, ?, ?, ?)" ) or die "Can't prepare statement: $DBI::errstr";
> $sth->execute($baseitemid, $langid, $title, $cost, $available)
> or die "Can't execute statement: $DBI::errstr";
> } # else if there was no duplicate item
>
> When I execute this with trace(2) set, I get this section of the output:
> <- prepare= DBI::st=HASH(0x828b1f0) at loadInventory.pl line 67
> -> execute for DBD::mysql::st (DBI::st=HASH(0x828b1f0)~0x81e27e0 '1' '1')
> -> dbd_st_execute for 0828b220
> Binding parameters: SELECT baseitemid FROM langversions WHERE baseitemid ='1' AND
> langid='1'
> <- dbd_st_execute 0 rows
> <- execute= '0E0' at loadInventory.pl line 68
> -> rows for DBD::mysql::st (DBI::st=HASH(0x828b1f0)~0x81e27e0)
> <- rows= '0' at loadInventory.pl line 70
> Use of uninitialized value in concatenation (.) or string at ./loadInventory.pl line 70.
> Duplicate item found at line 1:
>
> The last line is my STDERR output. The previous line is an error message. The trace() output
> seems to show rows() being set to '0' at line 70, which is "if ($sth->rows() == 0) {".
>
> I don't understand perl says $sth->rows() isn't initialized, when it seems to be set to zero,
> and why this system isn't working in this program, but seems to work fine in my other one.
>
> Thanks for any light you can shed on this problem.
>
> -Kevin
>
> --
> MySQL Perl Mailing List
> For list archives: http://lists.mysql.com/perl
> To unsubscribe: http://lists.mysql.com/[email protected]
>
>
__________________________________
Do you Yahoo!?
Yahoo! Small Business - Try our new resources site!
http://smallbusiness.yahoo.com/resources/
--
MySQL Perl Mailing List
For list archives: http://lists.mysql.com/perl
To unsubscribe: http://lists.mysql.com/[email protected]