Like the monks have said, DBI is the way to go. This is the
standard way to connect to a database in Perl, and has been for a
while. This example uses DBI to do the same thing with a mysql
database, the only difference in using Oracle is that the connection
routine would have to change and you would have to use DBD::Oracle.
(you have to set some env variables when connecting, ORACLE_HOME, and ORACLE_SID, i
believe, but check out the docs).
Anyway, the get_catnames() routine which returns a reference to an array
which is handed over to the CGI.pm popup_menu() function would work
just the same in oracle, providing it was working with a valid $dbh.
#!/usr/bin/perl -Tw
use strict;
use CGI qw# :standard :form #;
use DBI;
use DBD::mysql;
my $dbh = get_handle('dbname', 'dbuser', 'dbpasswd');
my $listref = get_catnames( \$dbh ); # sending a ref
print header;
print start_html;
print popup_menu( -name=>'categories', -value=>$listref );
$dbh->disconnect;
#####
#####
sub get_catnames {
my $dbh = shift;
my @cats = ();
my $statement = "select category_name from mtx_catnames";
my $sta = $$dbh->prepare($statement); # deref this
$sta->execute;
my $cats = $sta->fetchall_arrayref;
for my $row ( @{ $cats } ) {
push @cats, $row->[0];
}
return \@cats;
}
sub get_handle {
my ($DB, $DBUSER, $DBPASS) = @_;
my $dsn = "DBI:mysql:database=$DB;host=localhost";
my $dbh = DBI->connect($dsn, $DBUSER, $DBPASS)
or die( qq|cannot connect to database $DB| );
return $dbh;
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.