#!/usr/bin/perl use DBD::mysql; use DBI; use Utils::DBClass; $object = new DBClass( "perltest", "localhost", 3306,"root", "sierra"); $object -> select_db ("select * from samples"); #### package DBClass; use DBI; @ISA = ('Exporter'); @EXPORT_OK = ("Connection_check", "new","select_db"); sub Connection_check { my($self ) = @_; $dsn = "DBI:mysql:database=$self->{_db};host=$self->{_host};port=$self->{_port}"; $dbh = DBI->connect($dsn,$self->{_user},$self->{_pass}); return ($dbh) } sub new { my $class = shift; my $self = { _db => shift, _host => shift, _port => shift, _user => shift, _pass => shift }; $dbh = Connection_check($self); $self->{_dbh} = $dbh; bless $self, $class; return $self; } ### Retrieving the Rows ######## sub select_db{ my ($obj,$sel) = @_; my $sth1 = $dbh->prepare($sel); $sth1->execute or die "SQL Error: $DBI::errstr\n"; my @row; while (@row = $sth1->fetchrow_array) { print "@row \n"; } }