Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: DBI Style Inquiry

by mje (Curate)
on Jun 27, 2013 at 08:49 UTC ( #1040960=note: print w/ replies, xml ) Need Help??

in reply to DBI Style Inquiry

How about (untested)

sub get_loc { chomp $_[0]; my $dbh = DBI->connect("DBI:mysql:tracker:", 'myuser', 'mypassword', {RaiseError => 1}); my $a = $dbh->selectall_arrayref(q/SELECT location FROM servers WH +ERE name=? LIMIT 1/, undef, $_[0]); $dbh->disconnect; return $a->[0][0]; }

I've no idea why in your example you'd return before disconnecting as even though your $dbh goes out of scope you had error checking on the disconnect. RaiseOnError causes a die on error avoiding all those "or die". finish is not required if you fetch all off the result-set. selectall_arrayref is less code than prepare/execute.

Do you really want to connect and disconnect each time? If you were doing this alot you could prepare the query once and pass the sth to selectall_arrayref.

Comment on Re: DBI Style Inquiry
Download Code
Replies are listed 'Best First'.
Re^2: DBI Style Inquiry
by edict (Novice) on Jun 27, 2013 at 09:03 UTC
    Wow, that is much cleaner and clear than what I had going. Thanks for pointing out the flaws. I still have much to learn and it's awesome having access to a community so willing to assist.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1040960]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2015-11-27 00:30 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (710 votes), past polls