in reply to Count Number of rows retrieved from SELECT

Um, the way I do it is to modify the SELECT statement to do the counting for me. i.e.
$sql = "SELECT count(*) FROM table WHERE name = 'Jamie'"; $sth = $dbh->prepare( $sql ); if ( $sth && $sth->execute() ) { $row = $sth->fetchrow_arrayref(); print( "Returned " . $row->[0] . " row(s)\n" ); } else { print( "Error " . $dbh->errstr() . "\n" ); exit( 1 ); }

Replies are listed 'Best First'.
Re^2: Count Number of rows retrieved from SELECT
by dtharby (Acolyte) on Jun 13, 2005 at 12:00 UTC
    I was hoping not to have to do another SELECT statment just to get the number of rows that was pulled with the first statement.
    Oh well... will continue to play, there must be a way
      Can you include a counter (this is how I've done it in the past..)?
      my $count = 0; while ( my $row = $sth->fetchrow_arrayref() ) { $count++; # do something with $row.. } print( "There were $count row(s)\n" );

      I know what you're asking and it's a worthy question, I just haven't tried working it out for myself because I've just used either of the two techniques I've mentioned instead.

        thanks monarch... I will use that .... should have thought of that earlier!!! thanx