Re: DBD::mysql problem

From: Justin Wyllie (justin_at_notospammms-oxford.com)
Date: 11/24/04


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



Relevant Pages

  • Insert text containing and
    ... how do i insert the following data into my sql database using the INSERT statement... ... It is these single quotation marks, ... INSERT INTO table (adress) values ... Die, die Binär verstehen, bzw. ...
    (microsoft.public.sqlserver.programming)
  • Re: Insert text containing and
    ... Are you closing and reopening the database connection after each insert when you parameterize it? ... Stored proc wouldn't speed things up any more than a properly parameterized SQL query. ... "Expert SQL Server 2008 Encryption" ... Die, die Binär verstehen, bzw. ...
    (microsoft.public.sqlserver.programming)
  • Re: Insert text containing and
    ... Die, die Binär verstehen, bzw. ... The string concatenation method is pervasive and is why SQL injection vulnerabilities is common. ...
    (microsoft.public.sqlserver.programming)
  • cannot get the values specified inside of another file using include or require_once
    ... var $db; ... die("Could not connect to the database"); ... function updateName($id, $name){ ... the following sql: ".$sql); ...
    (comp.lang.php)
  • Re: SQL Server 2000 Clustered stopped
    ... Sql version: ... Running normal processes, like insert, delete, update. ... I really don't know what process are running because the server die:( ... Thanks Mike. ...
    (microsoft.public.sqlserver.clustering)