#!/usr/bin/perl
#
# PERL connecting to database via DBI
use DBI;

#/* connecting to a PostgreSQL database  */

$dbh =
DBI->connect("DBI:Pg:dbname=timetable;host=cs-linux;","mickey","MoUsE")
or
die ("Connect failed");

print 'Enter part of name for search: ';
$search = <STDIN>;
chomp ($search);

# construct an SQL statement
$what = 'name';
$statement = "select given, name, major, email from students  where $what
like '%$search%' order by name";
$sth = $dbh->prepare($statement)
    or die "Can't prepare $statement: $dbh->errstr\n";

$rv = $sth->execute
          or die "can't execute the query: $sth->errstr";

# /* Printing results  */

while(@row = $sth->fetchrow_array) {

        foreach (@row) {	# sets default variable to each column
            print "$_\t";	#    prints it ($_)
        }
        print "\n";
    }

 #   /* Free resultset */
$rc = $sth->finish;

#    /* Closing connection */
$rc = $dbh->disconnect;
