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

Discussion in 'Perl Misc' started by froil, Mar 6, 2006.

  1. froil

    froil Guest

    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;
    }
    froil, Mar 6, 2006
    #1
    1. Advertising

  2. froil

    Matt Garrish Guest

    "froil" <> wrote in message
    news:...
    >I am looking for the best way to limit the results to the first 10 that
    > are recieved from the database.


    LIMIT 10

    It's always best to check the documentation for whatever you're doing first:

    http://dev.mysql.com/doc/refman/5.0/en/select.html

    Matt
    Matt Garrish, Mar 6, 2006
    #2
    1. Advertising

  3. froil wrote:
    > 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
    it_says_BALLS_on_your_forehead, Mar 6, 2006
    #3
  4. froil

    Paul Lalli Guest

    froil wrote:
    > 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
    Paul Lalli, Mar 6, 2006
    #4
  5. froil

    Keith Keller Guest

    On 2006-03-06, froil <> wrote:
    > 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

    --
    -francisco.ca.us
    (try just my userid to email me)
    AOLSFAQ=http://wombat.san-francisco.ca.us/cgi-bin/fom
    see X- headers for PGP signature information
    Keith Keller, Mar 6, 2006
    #5
  6. In article <>, says...

    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

    --
    Heinrich Mislik
    Zentraler Informatikdienst der Universitaet Wien
    A-1010 Wien, Universitaetsstrasse 7
    Tel.: (+43 1) 4277-14056, Fax: (+43 1) 4277-9140
    Heinrich Mislik, Mar 6, 2006
    #6
  7. froil <> wrote:

    > 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 ...


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Mar 6, 2006
    #7
  8. froil

    Guest

    "froil" <> wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Mar 6, 2006
    #8
  9. froil

    froil Guest

    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);
    }
    }
    froil, Mar 6, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    1,065
    Victor Bazarov
    Jun 28, 2005
  2. Peter
    Replies:
    1
    Views:
    2,179
    John B. Matthews
    Jan 19, 2010
  3. Warren Seltzer
    Replies:
    7
    Views:
    132
    Darshan Patil
    Nov 11, 2005
  4. Tony De
    Replies:
    14
    Views:
    200
    Jesús Gabriel y Galán
    Mar 31, 2008
  5. sick dick
    Replies:
    0
    Views:
    381
    sick dick
    Apr 3, 2006
Loading...

Share This Page