CyberThinc
Perl example

This simple example demonstrates how a Perl CGI script can be used to retrieve a value from a database using a SQL Select statement. In this example, an Oracle database is used.

Code

#!/usr/bin/perl -w

use strict;

use CGI;
use DBI;

my $co = new CGI;

# Construct connection string to Oracle database
my $dbh = DBI->connect('dbi:Oracle:', $ENV{'DATABASE_USER'}, $ENV{'DATABASE_PASSWORD'});

# Check for failure condition
die ($DBI::errstr) unless $dbh;

# Prepare a SQL statement for execution and store it in a file handle called $table_cursor
my $table_cursor = $dbh->prepare("select table_field1 from table where table_field2='xxxx'");
die ($DBI::errstr) unless $table_cursor;

# Execute the SQL Statment contained in the $table_cursor file handle
$table_cursor->execute;
die ($DBI::errstr) unless $table_cursor;

my $table_count=0;
$table_count=$table_cursor->fetchrow;
die ($DBI::errstr) unless $table_count;

print $co->header,

$co->start_html(-title=>'CGI'),

$co->center($co->h1('Welcome to CGI')),

$co->center($co->h1('cache refresh test 12')),

$co->center($co->h1($table_count)),

$co->end_html;

$table_cursor->finish;

www.cyberthinc.com

© CyberThinc Limited 1996-2004