develooper Front page | perl.beginners | Postings from April 2002

Re: perl / database question

Thread Previous | Thread Next
From:
Elaine -HFB- Ashton
Date:
April 2, 2002 10:15
Subject:
Re: perl / database question
Message ID:
20020402181535.GM3494@chaos.wustl.edu
Johnson, Shaunn [SJohnson6@bcbsm.com] quoth:
*>Yes I am very new at this ... I was hoping I
*>could learn from some examples floating
*>around ... but I'm not sure where my results
*>should merge with the code (successfully).
*>
*>The 'while' statement: I want to say 'while
*>reading from this list of tables, create
*>a scrolling window and populate it with
*>the results from the sql query from above'.
*>
*>Are there any examples as to how to 
*>do this?

There are precious few out there as I went looking for a bare bones
example of something very similar for the CPAN Mirror page since I am not
a Web/CGI person and came up completely empty save for the examples from
the Perl DBI book http://examples.oreilly.com/perldbi/

I use MySQL but I think the Postgres fretchrow is very similar to the
MySQL fetchrow so you'll have something like:

my $sth = $dbh->prepare( "
            SELECT name,summary
            FROM list
            ORDER by name
          " );
$sth->execute;

while ( my ( $name, $summary ) = $sth->fetchrow_array ) {
    print <<EOF;
<TR><TD>$name</TD>
</TR>
EOF
  }

similarly for a select list/pull-down menu list

    my $sth = $dbh->prepare( "
                SELECT DISTINCT category
                FROM list
                ORDER by category
              " );
    $sth->execute();
    my %categories;

    while ( my ( $category ) = $sth->fetchrow_array ) {
@categories{split(' ', $category)} = ();
}
    for my $category (sort keys %categories) {

print<<EOF;
<OPTION  VALUE="$category">$category</OPTION>
EOF
}
print<<EOF;
</SELECT></P>
<P><INPUT TYPE="submit" VALUE="Category"></P>
</FORM>

where you make the query then dump out the contents in a while loop with a
HERE doc. Your mileage may vary, but that's basically it :)

e.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About