CERN - European Organization for Nuclear Research     Web Services  
Thursday, 25 April 2024
 

7. "Connecting to a Database"

Using databases from within your script is for advanced users only. It  is non-trivial and full of traps, so be aware that you have to know what you are doing, otherwise better leave it !!! 

Database connections can be done using the Perl DBI database module.

The example below is meant as an sketch only, don't copy and paste and expect it to work unless you really know waht you are doing !

NB: There is no programming support for these type of CGI-programming !!!


use DBI;
use strict;

my $query=new CGI;
print $query->header();

print "<html><head><title>DBI demo</title></head><body><h1>DBI/DBD Test</h1>\n";

my $dbh = DBI->connect( 'dbi:Oracle:YOUR-DATABASE-NAME',
'username', 'password', { RaiseError => 1, AutoCommit => 0 }
) || die "Database connection not made: $DBI::errstr";

my $sql = qq{ SELECT * FROM YOUR-TABLE };
my $sth = $dbh->prepare( $sql );

$sth->execute();

my( $name);

$sth->bind_columns( \$name, undef, undef );

while( $sth->fetch() ) {
print "$name <br>\n";
}

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


print "<body></html>";

 

 

 


Continue to: ...
 

Send feedback to: Web Support
Last update: Monday, September 15, 2003 06:06:09 PM
Web Services
© CERN /
IT / Internet Services