Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Test mysql connection, and return nice message?

by ultranerds (Hermit)
on Mar 13, 2010 at 14:49 UTC ( [id://828446]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

I'm trying to write an install script, and need to test if the mySQL DB details the give are ok. Now, to do this I'm using:

my $dbh = DBI->connect( "dbi:mysql:$db_name;host=$db_host", $db_user, $db_pass, { RaiseError => 1, AutoCommit => 0 } ) || push @errors, "Database connection not made +: $DBI::errstr";


The problem seems to be, that it never pushes that error into @errors for me though (just gives a fatal error, as I use fatalsToBrowser)

Any suggestions?

TIA

Andy

Replies are listed 'Best First'.
Re: Test mysql connection, and return nice message?
by almut (Canon) on Mar 13, 2010 at 14:59 UTC
    it never pushes that error into @errors for me though (just gives a fatal error

    With RaiseError => 1 you're telling DBI->connect() to raise an exception (i.e. die) in case of an error, rather than returning undef in combination with having set an error string.

    So I think you should just get rid of it...

      Ah thanks - that did the trick <G>

      Thanks again!

      Andy
Re: Test mysql connection, and return nice message?
by liverpole (Monsignor) on Mar 13, 2010 at 14:54 UTC
    Hi ultranerds,

    Have you seen the section in CGI::Carp called DOING MORE THAN PRINTING A MESSAGE IN THE EVENT OF PERL ERRORS?  I think that may be what you want -- it explains how to write an error handler for your specific needs.

    Instead of the clause:

    || push @errors, "Database connection not made: $DBI::errstr";
    you can apparently setup your own error handler to which you could pass just the error message, and have it push the error onto your error stack.

    Good luck!


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-03-28 22:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found