koti688 has asked for the wisdom of the Perl Monks concerning the following question:

how to get the column names and the values of them in select statement.

I have a package like this named ADB.pm
package ADB; use DBI; @ISA = ('Exporter'); @EXPORT_OK = ("Connection_check", "new","select_db"); sub new { my $class = shift; my $self = { _pf => shift, _db => shift, _host => shift, _port => shift, _user => shift, _pass => shift }; $dbh = Connection_check($self); #$self->{_dbh} = $dbh; bless $self, $class; return $self; } sub Connection_check { my($self ) = @_; $dsn = "DBI:$self->{_pf}:$self->{_db};$self->{_host};$self->{_port +}"; #$dsn = "DBI:mysql:database=$self->{_db};host=$self->{_host};port= +$self->{_port}"; $dbhc = DBI->connect($dsn,$self->{_user},$self->{_pass})or die "Un +able to connect: $DBI::errstr\n"; return ($dbhc) } ### Retrieving the Rows ######## sub select_db{ my ($obj,$sel) = @_; my $selnew = $dbh->prepare($sel); $selnew->execute or die "Unable to connect: $DBI::errstr\n"; my @AoA; my @FinArr; while (@AoA = $selnew->fetchrow_array) { push @FinArr, [@AoA]; } return @FinArr; }
and perl file adb.pl like this
#!/usr/bin/perl use DBI; use DBD::mysql; use Utils::ADB; $object = new ADB( "mysql","adb", "192.168.106.218", 3306, "root", "ro +ot"); my @records = $object -> select_db ("select * from users"); foreach $recRef (@records) { print join (" ", @$recRef),"\n"; } print "\n \n";
when execute adb.pl it displays the table results blidly.

I need to get the column names also with the respective values. Can u please tell me which modification i need to do in the code

results what i am getting are like this

1 admin admin 1 3 3
2 demouser 111111 1 1 1

i want the column names to be print for them.

Replies are listed 'Best First'.
Re: Small Doubt regarding DB Select
by mje (Curate) on Dec 10, 2008 at 09:12 UTC
Re: Small Doubt regarding DB Select
by Anonymous Monk on Dec 10, 2008 at 07:29 UTC
    when execute adb.pl it displays the table results blidly.
    What is "blidly"?

    I need to get the column names also with the respective values. Can u please tell me which modification i need to do in the code
    Who told you fetchrow_array would give you that? You'll have to use another method from DBI.