Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Small Doubt Regarding Select Staement in DB

by koti688 (Sexton)
on Nov 03, 2008 at 09:19 UTC ( [id://721043]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks, I have two files like below. 1st file : mail.pl
#!/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");
2nd file DBClass.pm in a folder named Utils.
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"; } }
I am Getting the output for the quiries i mentioned above as below: <html> 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 </html> 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 .

Replies are listed 'Best First'.
Re: Small Doubt Regarding Select Staement in DB
by rovf (Priest) on Nov 03, 2008 at 09:43 UTC
    my @row; while (@row = $sth1->fetchrow_array) { print "@row \n"; }
    i want to get the ouput using "return" function and using "array of arrays".

    Hmmmm.... you are nearly done. Inside the loop, you already have the data as an array. Though you can't exactly have "array of array", you can at least have either "array of arrayreference" or "reference to array of arrayreference", depending on your likings (or maybe depending on the calling context). If you define your result array initially as

    my @resultarray;
    you can successively push references to the rows into it, like this:
    push @resultarray,\@row;
    Then you can do one of these:
    return @resultarray; # returns list when called in list context return \@resultarray; # returns reference return wantarray ? @resultarray : \@resultarray; # adapt to calling co +ntext
    Although I personally use the latter type of return occasionally in my own code, I know that this usage is somewhat questionable, and at least needs to be documented well.

    -- 
    Ronald Fischer <ynnor@mm.st>
      Thanks Ronald.. I got it.. and i really appreciate ur explanation 2. Thanks a lot ~ koti.
Re: Small Doubt Regarding Select Staement in DB
by ikegami (Patriarch) on Nov 03, 2008 at 09:50 UTC

    @EXPORT_OK = ("Connection_check", "new","select_db");

    All three of those are methods. None of them should be exported.

      Thanks ikegami.. Itz veryuseful.
Re: Small Doubt Regarding Select Staement in DB
by ikegami (Patriarch) on Nov 03, 2008 at 09:35 UTC
    sub select_db{ my ($self,$sel) = @_; return $self->{_dbh}->selectall_arrayref($sel); }
      I should have mentioned it's pretty dangerous to do things this way since you can't avail yourself of replaceable parameters. Here are two alternate solutions:
      • Let the user call selectall_arrayref (or whatever):

        sub dbh{ my ($self) = @_; return $self->{_dbh}; }
      • Accept the same parameters as selectall_arrayref:

        sub select_db{ my $self = shift; return $self->{_dbh}->selectall_arrayref(@_); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://721043]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-24 01:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found