Best way to limit results to the first 10 recieved from database

F

froil

I am looking for the best way to limit the results to the first 10 that
are recieved from the database. Below is the code i know that i need
to add a counter i am just wondering where the best most effective way
to use it would be. can you pass two arguements in a while loop?
thanks in advance.
#!/usr/bin/perl -wT
use strict;
use DBI;
use CGI qw:)standard);
use CGI::Carp qw(fatalsToBrowser);
my $Url_Path = "/journalpics";
my $username = '?';
my $password = '?';
my $data_source = 'DBI:mysql:jilesfr_pictures:69.6.255.192:3306';
Print_Html_Top();
Get_Descriptions();
Print_Html_Bottom();
sub Print_Row{
my $rec = shift;
my $Picture = $rec->{Picture};
my $User = $rec->{User};
my $Blog = $rec->{Blog};
my $Date = $rec->{Date};
print qq(<tr><td><p align=center><img
src=$Url_Path/$Picture></p></td></tr>);
print qq(<tr><td><P align=center><FONT
size=5>$Date</FONT></P></td></tr>);
print qq(<tr><td><P align=center><FONT size=3>posted by:
$User</FONT></P></td></tr>);
print qq(<tr><td><P align=center><Font
size=4>$Blog</font></p></td></tr>);
print qq(<tr height=10><td></td></tr>);
}
sub Get_Descriptions{
my $DBH = DBI->connect( $data_source, $username, $password )
or die "Error: $DBI::errstr\n";
my $sth_fetch =
$DBH->prepare( qq(SELECT * FROM Blog) ) or die
$DBH->errstr;
$sth_fetch->execute();
while( my $ptr = $sth_fetch->fetchrow_hashref ){
Print_Row($ptr);
}
}
sub Print_Html_Top {
print header;
print start_html("JilesFamily.net");
print qq(<table border=1>);
}
sub Print_Html_Bottom{
print qq(</table>);
print end_html;
}
 
I

it_says_BALLS_on_your_forehead

froil said:
I am looking for the best way to limit the results to the first 10 that
are recieved from the database. Below is the code i know that i need
to add a counter i am just wondering where the best most effective way
to use it would be. can you pass two arguements in a while loop?
thanks in advance.

$DBH->prepare( qq(SELECT * FROM Blog) ) or die

SELECT * FROM Blog where rownum < 11
 
P

Paul Lalli

froil said:
I am looking for the best way to limit the results to the first 10 that
are recieved from the database.

Er. Why not just limit the database to only return the first 10 rows?

http://dev.mysql.com/doc/refman/4.1/en/select.html#id3037996
Below is the code i know that i need
to add a counter i am just wondering where the best most effective way
to use it would be. can you pass two arguements in a while loop?
thanks in advance.

my $i = 0;
while( my $ptr = $sth_fetch->fetchrow_hashref ){
Print_Row($ptr); last if $i++ == 10;
}

Paul Lalli
 
K

Keith Keller

I am looking for the best way to limit the results to the first 10 that
are recieved from the database.

This is not a Perl solution, but why not use LIMIT 10 in your SQL query?

--keith
 
H

Heinrich Mislik

I prefer selectall_arrayref with MaxRows like this (untested):

sub Get_Descriptions{
my $DBH = DBI->connect( $data_source, $username, $password )
or die "Error: $DBI::errstr\n";
my $sth_fetch =
$DBH->prepare( qq(SELECT * FROM Blog) ) or die $DBH->errstr;



# $sth_fetch->execute();
my $result = selectall_arraref($sth_fetch,
{Slice => {}, MaxRows => 10}) or die $DBH->errstr;
# while( my $ptr = $sth_fetch->fetchrow_hashref ){
# Print_Row($ptr);
# }
for my $ptr (@$result){
Print_Row($ptr);
}
}

Cheers

Heinrich
 
T

Tad McClellan

froil said:
I am looking for the best way to limit the results to the first 10 that
are recieved from the database.


You can do that in SQL.

Below is the code i know that i need
to add a counter


You do not need to add a counter.

$DBH->prepare( qq(SELECT * FROM Blog) ) or die

$DBH->prepare( qq(SELECT * FROM Blog LIMIT 10) ) or die ...
 
X

xhoster

froil said:
I am looking for the best way to limit the results to the first 10 that
are recieved from the database.

Best according to what criteria? Best performance, DB portability be
damned? Best DB portability, performance be damned (which, some would say,
is the only way to achieve portability)? Hardest to screw up the
implementation of?
my $sth_fetch =
$DBH->prepare( qq(SELECT * FROM Blog) ) or die
$DBH->errstr;

No "order by"?

Xho
 
F

froil

just in case someone else needs follow up in the future i went with SQL
Limit. probably easiest not to mess up:) and ofcourse Xho got the
other part of the statement i had to add to make it work like i really
wanted:)

sub Get_Descriptions{
my $DBH = DBI->connect( $data_source, $username, $password )
or die "Error: $DBI::errstr\n";
my $sth_fetch =
$DBH->prepare( qq(SELECT * FROM Blog ORDER BY
primary_key DESC LIMIT 0,10) ) or die $DBH->errstr;
$sth_fetch->execute();
while( my $ptr = $sth_fetch->fetchrow_hashref ){
Print_Row($ptr);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top