http://qs321.pair.com?node_id=218400

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

#!/fellow/monks.pl

I have a slight problem connecting via DBI to a mySQL database. Maybe I need to clarify: My connection problem is with mySQL, which I'm on top of, my problem is that the CGI dies, with the error message in the error_log file of Apache. I would like to display this error message via the web browser. Now my question... How can I detect if the DBI connection has failed, and then print the error message out?

#!/usr/bin/perl use DBI; print "Content-type: text/html\n\n"; $server = "127.0.0.1"; $sid = "mysid"; $user = "myuser"; $passwd = "mypassword"; $dbh = DBI->connect("DBI:Oracle:host=$server;sid=$sid", $user, $passwd +); ... ...

Thanks!

#!/massyn.pl

You never know important a backup is until the day you need it.

Replies are listed 'Best First'.
Re: Detecting a failed DBI connection
by BrowserUk (Patriarch) on Dec 08, 2002 at 22:25 UTC

    You should look at use CGI::Carp qw/FatalsToBrowser/;

    It's generally considered bad form to leave this in live code, but it's very useful during development.


    Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
    Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
    Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
    Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

Re: Detecting a failed DBI connection
by pfaut (Priest) on Dec 08, 2002 at 22:27 UTC

    It looks like you might need to add some error checking to your program. Something like the following will display the error text if the connection fails.

    $dbh = DBI->connect("DBI:...", $user, $passwd) or print "<p>Error connecting to database: " . $dbh->errstr . "</p +>", exit;

    Look around the monastery for DBI tutorials. I've seen a few but don't have any bookmarks handy.

      #!/fellow/monks.pl

      Thanks! You put me on the right track, but the code didn't work. For some reason, $dbh->errstr doesn't cut it. I looked it up, and found that $DBI::errstr did the trick.

      Thanks!

      #!/massyn.pl

      You never know important a backup is until the day you need it.

      If $dbh isn't defined, call errstr() on it? :)

        oops!

      I'm just wondering as to whether your way of printing out errors to the browser is efficient. For starters, this is really just reinventing the fatalsToBrowser method provided by the CGI::Carp module. I could understand this if maybe you didn't want all errors to be outputted to the user's browser. But even at this point, you'd have to make sure that HTTP headers have already been sent to the browser, otherwise your code would just damage the output. If you're set on using this, maybe a subroutine call would be more appropriate, so that you can later change the error handling behaviour. Besides, then you won't have ugly exit() calls hanging around everywhere :)

      mt2k -> must try 2 know

        I wouldn't write code this way but the user's example had not used the CGI module and had already put out the http header. The original poster wanted to know how to find out what went wrong so I plugged an [unfortunately broken] example of how to find out into his code. I guess I could have gone on to give a couple of pointers on better ways to structure his web apps and rewritten his entire snippet for him but I thought he's already having problems with DBI, why confuse him with talk about CGI? Hopefully, he'll get there when he's ready for it.