in reply to DBI, From PHP to Perl

As tantarbobus as mentioned already, you can't have a count of rows beforehand with MySQL, unless you make sure you are using mysql_store_result (which is the default mode with DBD::mysql, BTW).

In PHP it's the same story. PHP is hiding complexity from you. If the method mysql_num_rows() works, it's only because PHP uses always mysql_store_result(). If you read the relevant explanation on MySQL manual you'll see that a count of rows is only possible after you are finished fetching them.

That said, If you want to be sure of the row number and you don't want to use a separate COUNT(*), then the only way is to use one of the "fetchall_*" or "selectall_*" methods from the DBI.

my $sth = $dbh->prepare($sql); $sth->execute || $error->LogError("whatever ".$dbh->errstr, 0); my $recs = $sth->fetchall_arrayref; my $count = @$recs;

Once you have the records stored in an array, the number of records is given, as for any array in Perl, testing the array in scalar context.

Replies are listed 'Best First'.
Re: Re: DBI, From PHP to Perl
by dstefani (Sexton) on Nov 08, 2003 at 20:41 UTC
    Thank you all for your input.
    I'm trying to learn accurate and elegant ways of coding, although at this point I'm about as elegant as my two year old with a bowl of soup.

    I do appreciate your patients with a newbie.

    Thanks,
    dstefani