There in fact are method(s) that limit the number of results you get. They're called fetchrow_*(). They limit the number of results to one row so you can control however many you actually obtain. Straight out of the DBI documentation:
$sth = $dbh->prepare("SELECT foo, bar FROM table WHERE baz=?");
$sth->execute( $baz );
while ( @row = $sth->fetchrow_array ) {
print "@row\n";
}
Now you can transform this a little to suit your needs like merlyn was talking about:
my $LIMIT = 30;
my $sth = $dbh->prepare("SELECT foo, bar FROM table WHERE baz=?");
$sth->execute( $baz );
my ( @rows, @row );
for ( 1 .. $LIMIT ) {
last unless ( @row = $sth->fetchrow_array );
push @rows, [ @row ];
}
#do stuff with @rows
This code is untested but you get the general idea. There are other types of fetchrow_*() methods that you can take a look at. Read the DBI documentation.
Update: You can also take a look at the fetchall_arrayref() method which appears to take a $max_rows parameter.
Zenon Zabinski | zdog | zdog@perlmonk.org
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|