in reply to DBI Style Inquiry
How about (untested)
sub get_loc { chomp $_[0]; my $dbh = DBI->connect("DBI:mysql:tracker:12.34.56.78", '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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: DBI Style Inquiry
by edict (Novice) on Jun 27, 2013 at 09:03 UTC |