: I am also a beginner and wondering if there are any other recommended
: books other than the camel book from O'Reilly.  I have been asked to
: display information from the Oracle Database on the web using Perl and
: CGI.  So far, I mostly know how to check for patterns.  Gulp!!

O'Reilly has also published a book on DBI ("Programming the Perl DBI"),
which is Perl's standard way of accessing relational databases.

Of course,nothing beats an example for getting you started.  This one's
for Sybase, but I'm sure someone out there could amend the connection
string for Oracle; I'm sure they'll offer their comments on the style
:) Assumes that you have the DBI module installed and the proper
database driver (for this example, DBD::Sybase; for you, DBD::Oracle).
It takes one CGI parameter from a form, uses it in a SQL query, runs
the query, and displays some results in a dull format. Very little
error checking or input validation; that's up to you. ;)

-- tdk

#!/usr/bin/perl

use strict;
use DBI;
use CGI;

my $cgi = new CGI;

#
# Create a database handle; this represents a connection
# to the database server
#
my $dbh = DBI->connect("dbi:Sybase:server=ZEPPO;database=dadsops", "myUsername", 
"myPassword")
        or die DBI->errstr;

#
# Create a statement handle- this represents a query
# running on the server- and give it the SQL query.
#
my $sth = $dbh->prepare("SELECT * FROM science WHERE sci_pi_last_name = ?")
        or die DBI->errstr;

#
# Do it.
#
$sth->execute( $cgi->param("pi_last_name") )
        or die DBI->errstr;

#
# Print the HTTP header and opening HTML
#
print STDOUT $cgi->header;
print STDOUT $cgi->start_html(
        -title => 'Monster CGI script',
        -bgcolor => 'green',
);

print STDOUT "<h1>Some data</h1>\n";
print STDOUT "<pre>\n";

#
# Loop through the results as they come back
# from the database. $row will be a hash reference
# keyed on the column names. Here', we're just printing
# the content of one of the columns.
#
while ( my $row = $sth->fetchrow_hashref ) {
        print STDOUT $row->{sci_data_set_name}, "\n";
}

print STDOUT "</pre>\n";
print STDOUT $cgi->end_html;

$sth->finish;
$dbh->disconnect;

Reply via email to