use strict; use warnings; use DBI; sub main { # Connect to DB my $dbh = DBI->connect("dbi:mysql:perl_test","cottage","c0ttage"); unless(defined($dbh)){ die "Cannot connect to database \n"; } # Prepare Select statement my $sql = "select * from contacts"; my $statement = $dbh->prepare($sql); unless($statement){ die "Error preparing SQL statement \n"; } # Execute Select statement if($statement->execute()) { print "Select Statement complete \n"; } # Bind Column values to variables my $id; my $fn; my $ln; my $amt; my $dt; $statement->bind_columns(undef, \$id, \$fn, \$ln, \$amt, \$dt); # Loop through the select statement results while($statement->fetch()){ print "ID: $id First Name: $fn Last Name: $ln Amount: $amt Date: $dt \n"; } $dbh->disconnect(); print "Done - Disconnected from DB\n"; } main();