Date sent: Sun, 15 Jul 2001 19:52:15 -0400
Subject: DBI and HTML Display
From: Kevin Diffily <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Kevin
> Can anyone offer a simple example of retrieving information with DBI and
> displaying it in HTML? I am simply looking for the basic syntax.
There is no specific basic syntax for this conversion. DBI returns a
Perl data structure from your database which you can further process
as if you retrieved it, say, from a plain text file.
>
> I am new to DBI but have pretty extensive Perl and Website experience.
>
The following converts a complete table of your database into a
minimalistic Web page:
use strict;
use DBI;
my $dsn = 'your_connection_string_here'; # read the docs for this,
sorry
my $dbh = DBI->connect($dsn, {RaiseError => 1}) or die DBI::errstr();
my $sth = $dbh->prepare("SELECT * FROM your_table");
$sth->execute;
print "Content-type: text/html\n\n";
print '<html><head><title></title></head><body><table>';
print '<tr><th>', join( '</th><th>', @{$sth->{NAME}}) , '</th></tr>';
while (my @row = $sth->fetchrow_array) {
print '<tr><td>', join('</td><td>', @row), '</td></tr>';
}
print '</table></body></html>';
$dbh->disconnect;
Please note that this is just a *very* basic example with too much of
hardcoded html in it and that there are lots of modules for
accomplishing this task. Anyways, I think that there is no way round
reading the DBI pod over and over again for the next days...
Bodo
[EMAIL PROTECTED]
Dr. med. Bodo Eing
Institut fuer Medizinische Mikrobiologie
Klinische Virologie
v.-Stauffenbergstr. 36
48151 Muenster
Germany
Phone: ++49 251 7793 111 Fax: ++49 251 7793-104