List:General Discussion« Previous MessageNext Message »
From:Paul DuBois Date:January 10 2003 8:02pm
Subject:Re: What's returned from $sth->execute(); ?
View as plain text  
At 16:15 +0000 1/10/03, Jeff Snoxell wrote:
>Hi,
>
>I've been using the return value of $sth->execute(); (in Perl DBI) to
>determine if I have a result set. I'm not using the value I get back but am
>assuming that if it's >=1 then I have some results.

That's incorrect because it's valid for a result set to be empty (have
zero rows).  Consider the result of "SELECT * FROM t WHERE 1 = 0".


You don't need to use the return value at all.  Check the metadata to
see if the number of columns is zero.  If it is, there's no result set.
If it's > 0, there is a result set.

Some code to illustrate:

printf "Query: %s\n", $query;
my $sth = $dbh->prepare ($query);
$sth->execute();
# metadata information becomes available at this point ...
printf "NUM_OF_FIELDS: %d\n", $sth->{NUM_OF_FIELDS};
print "Note: query has no result set\n" if $sth->{NUM_OF_FIELDS} == 0;


>
>Is this a safe thing to do with MySQL?
>
>And
>
>The results do actually seem to be correct for the number of records I get
>back. Should this be the case? Is it reliable?

If you mean, can you interpret the $sth->execute() result as a row
count, the DBI docs specifically discourage it.

>
>Thanks,
>
>Jeff

Thread
Resetting the auto_increment to start from 1Octavian Rasnita8 Jan
  • Re: Resetting the auto_increment to start from 1Paul DuBois8 Jan
  • Re: Resetting the auto_increment to start from 1iConnect \(Berlin\) Stefan Hinz8 Jan
    • Re: Resetting the auto_increment to start from 1Paul DuBois9 Jan
      • What's returned from $sth->execute(); ?Jeff Snoxell10 Jan
        • Re: What's returned from $sth->execute(); ?Paul DuBois10 Jan
  • Re: Resetting the auto_increment to start from 1Octavian Rasnita9 Jan
  • Re: Resetting the auto_increment to start from 1iConnect \(Berlin\) Stefan Hinz9 Jan
Re: What's returned from $sth->execute(); ?Rich Allen11 Jan