#!/usr/local/bin/perl # $Id: ApacheDBI.pl,v 1.1 2004-04-29 09:21:28 ivan Exp $ # don't forget to create in postgres the user who is running # the httpd, eg 'createuser nobody' ! # # demo script, tested with: # - PostgreSQL-7.1.1 # - apache_1.3.12 # - mod_perl-1.23 # - perl5.6.0 # - DBI-1.14 use CGI; use DBI; use strict; my $query = new CGI; print $query->header, $query->start_html(-title=>'A Simple Example'), $query->startform, "

Testing Module DBI

", "

", "", "", "", "", "", "", "", "", "", "", "", "", "
Enter the data source: ", $query->textfield(-name=>'data_source', -size=>40, -default=>'dbi:Pg:dbname=template1'), "
Enter the user name: ", $query->textfield(-name=>'username'), "
Enter the password: ", $query->textfield(-name=>'auth'), "
Enter the select command: ", $query->textfield(-name=>'cmd', -size=>40), "

", "

", $query->submit(-value=>'Submit'), "
", $query->endform; if ($query->param) { my $data_source = $query->param('data_source'); my $username = $query->param('username'); my $auth = $query->param('auth'); my $cmd = $query->param('cmd'); my $dbh = DBI->connect($data_source, $username, $auth); if ($dbh) { my $sth = $dbh->prepare($cmd); my $ret = $sth->execute; if ($ret) { my($i, $ary_ref); print "

\n"; while ($ary_ref = $sth->fetchrow_arrayref) { print "\n"; } print "
", join("", @$ary_ref), "

\n"; $sth->finish; } else { print "

", $DBI::errstr, "

\n"; } $dbh->disconnect; } else { print "

", $DBI::errstr, "

\n"; } } print $query->end_html;