Click here to get back home

DBD::mysql problem

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
DBD::mysql problem Justin Wyllie 11-24-2004
Posted by Justin Wyllie on November 24, 2004, 12:24 am
Please log in for more thread options
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

With regards

Justin Wyllie




Posted by Jim Gibson on November 23, 2004, 6:06 pm
Please log in for more thread options

> 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.


Posted by Justin Wyllie on November 24, 2004, 5:33 pm
Please log in for more thread options
>
> > 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





Similar ThreadsPosted
Problem with DBD-mysql November 20, 2004, 2:34 am
problem installing DBD-mysql January 10, 2005, 5:51 pm
DBI Mysql insert problem October 12, 2007, 6:18 pm
Problem with set_sql in Class::DBI::Loader::mysql March 22, 2006, 2:47 pm
Mysql-DBD Perl module Installation Problem in HPUX June 30, 2005, 9:05 am
[RESOLVED] DBD::mysql unresolved symbol _intel_fast_memcpy & mysql-standard-5.0.22-linux-i686-icc-glibc23 August 29, 2006, 6:33 pm
MySQL 5.0 and Perl DBD-MySQL December 3, 2005, 1:10 am
Embedded mysql with DBD::mysql July 30, 2008, 10:15 am
DBD:mysql doesn't read mysql option file /etc/my.cnf file January 27, 2005, 11:19 pm
DBD::mysql and UTF-8 August 13, 2005, 11:27 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap