#! /usr/bin/perl

# dump_members2 - dump Historical League's membership list

use DBI;
use strict;

my ($dsn) = "DBI:mysql:samp_db:localhost"; # data source name
my ($user_name) = "paul";  # user name
my ($password) = "secret"; # password
my ($dbh, $sth);           # database and statement handles
my (@ary);                 # array for rows returned by query
my (%attr) =               # error-handling attributes
(
    PrintError => 0,
    RaiseError => 0
);

# connect to database
$dbh = DBI->connect ($dsn, $user_name, $password, \%attr)
	or bail_out ("Cannot connect to database");

# issue query
$sth = $dbh->prepare ("SELECT last_name, first_name, suffix, email,"
	. "street, city, state, zip, phone FROM member ORDER BY last_name")
	or bail_out ("Cannot prepare query");
$sth->execute ()
	or bail_out ("Cannot execute query");

# read results of query
while (@ary = $sth->fetchrow_array ())
{
	print join ("\t", @ary), "\n";
}
$DBI::err == 0
	or bail_out ("Error during retrieval");

# clean up
$sth->finish ()
	or bail_out ("Cannot finish query");

$dbh->disconnect ()
	or bail_out ("Cannot disconnect from database");
exit (0);

# bail_out() subroutine - print error code and string, then exit

sub bail_out
{
my ($message) = shift;

	die "$message\nError $DBI::err ($DBI::errstr)\n";
}
