Re: DBD::mysql problem
From: Justin Wyllie (justin_at_notospammms-oxford.com)
Date: 11/24/04
- Next message: rodneyr: "Re: GD make test problem"
- Previous message: John Bokma: "Re: mod_perl and autoloader"
- In reply to: Jim Gibson: "Re: DBD::mysql problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 24 Nov 2004 16:33:03 -0000
>
> > Hi
> >
> > I have a problem with one line of code here:
> >
> > $oldcount = $sth->fetchrow_array() or die("Error Number: 60");
> >
> > The problem is that when the SQL, which just gets a one column one row
value
> > has a value of 0 (zero) die is called - even though zero is a correct
and
> > valid value. I suppose the zero is being evaluated as false.
> >
> > My workaround is to get two columns from the SQL and throw away one and
a
> > result set with two columns cannot be evaluated as false. But I wondered
if
> > this was an inevitable result of using fetchrow_array in this way or
whether
> > there is some other way round this
>
> According to the documentation (see 'perldoc DBI'), fetchrow_array()
> returns an empty list if there are no rows or an error occurs. Although
> a single column value in the row may be zero, you should still get the
> number of columns in $oldcount, which should be 1 or more.
>
> However, if returning no rows is not an error, you should not die based
> on the return value from fetchrow_array(). Do what the documentation
> says instead: check the value of $sth-err or use the RaiseError
> attribute.
>
> Also heed the warnings in the documentation about calling
> fetchrow_array() in a scalar context. Maybe you should use (untested)
>
> my @oldrow = $sth->fetch_array();
> die $sth->errstr if $sth->err;
> my $oldcount = @oldrow;
>
> instead.
Hello Jim
Thanks. That was very illuminating.
$oldcount = $sth->fetchrow_array();
die $sth->errstr if $sth->err;
works fine. The main thing is it was evaluating the return value of 0 as
false and calling die. This isn't explicitly mentioned in the doc which
talks about Perl not being able to distinguish between the undef of an error
or the undef of an empty row.
Regards
Justin Wyllie
- Next message: rodneyr: "Re: GD make test problem"
- Previous message: John Bokma: "Re: mod_perl and autoloader"
- In reply to: Jim Gibson: "Re: DBD::mysql problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|