1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
#!/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,
"<CENTER><H3>Testing Module DBI</H3></CENTER>",
"<P><CENTER><TABLE CELLPADDING=4 CELLSPACING=2 BORDER=1>",
"<TR><TD>Enter the data source: </TD>",
"<TD>", $query->textfield(-name=>'data_source', -size=>40, -default=>'dbi:Pg:dbname=template1'), "</TD>",
"</TR>",
"<TR><TD>Enter the user name: </TD>",
"<TD>", $query->textfield(-name=>'username'), "</TD>",
"</TR>",
"<TR><TD>Enter the password: </TD>",
"<TD>", $query->textfield(-name=>'auth'), "</TD>",
"</TR>",
"<TR><TD>Enter the select command: </TD>",
"<TD>", $query->textfield(-name=>'cmd', -size=>40), "</TD>",
"</TR>",
"</TABLE></CENTER><P>",
"<CENTER>", $query->submit(-value=>'Submit'), "</CENTER>",
$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 "<P><CENTER><TABLE CELLPADDING=4 CELLSPACING=2 BORDER=1>\n";
while ($ary_ref = $sth->fetchrow_arrayref) {
print "<TR><TD>", join("</TD><TD>", @$ary_ref), "</TD></TR>\n";
}
print "</TABLE></CENTER><P>\n";
$sth->finish;
} else {
print "<CENTER><H2>", $DBI::errstr, "</H2></CENTER>\n";
}
$dbh->disconnect;
} else {
print "<CENTER><H2>", $DBI::errstr, "</H2></CENTER>\n";
}
}
print $query->end_html;
|