Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

DBI can't catch error

by gepebril69 (Beadle)
on Nov 26, 2012 at 13:07 UTC ( #1005643=perlquestion: print w/ replies, xml ) Need Help??
gepebril69 has asked for the wisdom of the Perl Monks concerning the following question:

Hi there,

I'm using DBI to connect to an MySQL database to report statuses from several machines. This works excellent except for some occasions. It seems DBI crashes on opening without producing an error, or am I using wrong code.

I don't want to use the die option as I like to close things myself and take action if neccesary

# Connect to MySQL server $Dbh = DBI->connect($DSN, $Config{'MYSQL_USER'}, $Config{'MYSQL_PASSWD +'}, { PrintError => 0, RaiseError => 0 }); if ($DBI::err) { if ($Dbh) { $Dbh->disconnect} print "Error! we had an error connecting to CMS DB: ".$DBI::errstr; + } # continue code

Thanks in advance

Albert

Comment on DBI can't catch error
Download Code
Re: DBI can't catch error
by mje (Deacon) on Nov 26, 2012 at 13:27 UTC

    If you fail to connect $Dbh will be undef. If $Dbh is not undef, you connected. So check $Dbh not $DBI::err.

      ok, that easy.

      So simplified it will be something like

      # Connect to MySQL server $Dbh = DBI->connect($DSN, $Config{'MYSQL_USER'}, $Config{'MYSQL_PASSWD +'}, { PrintError => 0, RaiseError => 0 }); # Check if connect went ok if (defined ($Dbh)) { # Good print "Successful connection with DB"; } else { # Problem! print "We had an error connecting to DB: ".$DBI::errstr; exit; }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (11)
As of 2014-09-17 11:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (72 votes), past polls