方式过于简单的例子:
# Get Some data $query = $db->prepare(qq{ select * from my_table where id = "Some Value" }); $query->execute; # Iterate through the results if ( *THE QUERY HAS RETURNED A RESULT* ) { print "Here is list of IDs "; while ($query_data = $query->fetchrow_hashref) { print "$query_data->{id}"; } };
在那里寻找“THE QUERY HURN RETURN A A RESULT”的代码.如果可能的话,我想避免在我的sql中使用count(*),因为这需要“group by”.
解决方法
my $sth = $dbh->prepare($stmt); $sth->execute(); my $header = 0; while (my $row = $sth->fetchrow_hashref) { print "Here is list of IDs:\n" if !$header++; print "$row->{id}\n"; }
替代方案:
my $sth = $dbh->prepare($stmt); $sth->execute(); my $row = $sth->fetchrow_hashref; print "Here is list of IDs:\n" if $row; while ($row) { print "$row->{id}\n"; $row = $sth->fetchrow_hashref; }
更简单的代码以牺牲内存为代价:
my $ids = $dbh->selectcol_arrayref($stmt); if (@$ids) { print "Here is list of IDs:\n"; print "$_\n" for @$ids; }