Question

I have seen many articles on this, and none are working for me. I have this code -

$var1->{reference} = sql_get_rows();   


sub sql_get_rows(){
    etc
    return ($sth->fetchall_arrayref());
 };

What I want to do is know how many rows are found in the SQL statement and stored in $var1->{reference} (or be told a better way to approach the problems. I have written what $var1->{reference} is storing below.

warn Dumper ($var1->{reference});

'$VAR1 = [
      [
        26412502,
        'initial'
      ],
      [
        49246682,
        'title'
      ],

Note that I know how to find the COUNT by an SQL statement, thats not what I want to know.

Thanks

Was it helpful?

Solution

If using DBI, use the rows method

my $sth = $dbh->prepare(q{...});
$sth->execute(@bound_variables) or die $dbh->errstr;

print "Rows = " . $sth->rows;

As @ThisSuitIsBlackNot pointed out, this may not work, so if you're pulling all of the records, just count the data structure:

print "Rows = " . scalar @{$var1->{reference}};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top