cd www
chmod 755 psql.pl
http://cubist.cs.washington.edu/~yourusername/psql.pl
#!/usr/bin/perl5 -w
use CGI;
use DBI;
First line says that this file should be executed using the binary perl5. It then needs to use the 'CGI' and 'DBI' libraries.
$html = new CGI;
print $html->header, "\n",
$html->start_html();
Next, it creates a new object of type CGI. This is your html page. First print its header (just by calling the 'header' function) and then some basic tags for the beginning of the page (just by calling start_html()).
print $html->end_html;
To properly terminate this HTML page, you need to call at the end the 'end_html()' function.
sub searchDatabase
{
$db = DBI->connect("dbi:Pg:dbname=cse444", "cse444", "CSE444");
$db or die "Sorry we couldn't connect!\n";
$sth = $db->prepare($sql);
$sth->execute();
$row = "";
while ($row = $sth->fetchrow_hashref()) {
}
The query statement is clear. To connect to the PostgreSQL server, you call the "DBI->connect" function, with the parameters as above. You then need to call "prepare" and "execute". Finally, you retrieve row by row, by calling the "fetchrow_hashref()" function. To extract the record use "$row->{lname}". While this is missing from the code, there obviously is a "$row->{fname}".