By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,710 Members | 1,960 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,710 IT Pros & Developers. It's quick & easy.

Small Doubt regarding DB-Select

P: 11
I have two files like below.

1st file : mail.pl

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use DBD::mysql;
  3. use DBI;
  4. use Utils::DBClass;
  5. $object = new DBClass( "perltest", "localhost", 3306, "root", "sierra");
  6. $object -> select_db ("select * from samples");
  7.  
2nd file DBClass.pm in a folder named Utils.


Expand|Select|Wrap|Line Numbers
  1. package DBClass;
  2. use DBI;
  3. @ISA = ('Exporter');
  4. @EXPORT_OK = ("Connection_check", "new","select_db","insert_db","delete_db","update_db");
  5. sub Connection_check {
  6.     my($self ) = @_;
  7.     $dsn = "DBI:mysql:database=$self->{_db};host=$self->{_host};port=$self->{_port}";
  8.     $dbh = DBI->connect($dsn,$self->{_user},$self->{_pass});
  9.     return ($dbh)
  10. }
  11. sub new
  12. {
  13.     my $class = shift;
  14.     my $self = {
  15.         _db => shift,
  16.         _host => shift,
  17.         _port => shift,
  18.         _user => shift,
  19.         _pass => shift
  20.     };
  21.     $dbh = Connection_check($self);
  22.     $self->{_dbh} = $dbh;
  23.     bless $self, $class;
  24.     return $self;
  25. }
  26. ### Retrieving the Rows ########
  27. sub select_db{
  28.   my ($obj,$sel) = @_;
  29.   my $sth1 = $dbh->prepare($sel);
  30.   $sth1->execute or die "SQL Error: $DBI::errstr\n";
  31.   my @row;
  32.   while (@row = $sth1->fetchrow_array)
  33.     {
  34.       print "@row \n";
  35.     }
  36. }
  37.  
I am Getting the output for the quiries i mentioned above as below:

1 Koti 555-5555
2 Siva 222-2222
3 Praneet 555-5555
4 nishitha 08649-257828
6 Anu 222-2222
7 Pradeep 555-5555
8 Asmit 222-2222
9 Surya 555-5555

My requirement is getting the out put with out using the "print" statement in the select_db function. i want to get the ouput using "return" function and using "array of arrays".

can anyone suggest me how to proceed .
Nov 3 '08 #1
Share this Question
Share on Google+
2 Replies


nithinpes
Expert 100+
P: 410
Replace :
Expand|Select|Wrap|Line Numbers
  1. while (@row = $sth1->fetchrow_array) 
  2.     { 
  3.       print "@row \n"; 
  4.     } 
  5.  
with:

Expand|Select|Wrap|Line Numbers
  1. my @AoA;
  2. while (@row = $sth1->fetchrow_array) 
  3.     { 
  4.       push @AoA, [@row] ;   #create array of array
  5.     } 
  6. return @AoA;    # return array of array
  7.  
  8.  
Nov 3 '08 #2

P: 11
Thanks A Lot nithinpes..
Nov 3 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.