Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Printing DBI errors from a CGI script

by PB (Initiate)
on Apr 18, 2002 at 19:53 UTC ( [id://160324]=perlquestion: print w/replies, xml ) Need Help??

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

Hi

I want to print an error if any DBI functions fail, but
all that happens is I get apache errors.

In the code below, if the SQL insert statement(or connect for that matter) fails I get an "Internal Server error" from apache instead of my print statement.
The apache logs explains why the error occured eg duplicate field, but I want to show the error on the browser.
use strict ; use CGI qw(param); use diagnostics ; sub init { # Constants my $db = "edb"; my $server = "localhost"; my $user = "localuser"; my $password = "localpasswd"; # CGI Parameters my $Id = param("ID"); my $Fname = param("Fname"); # Connect to database $dbh->{RaiseError} = 1; my $dbh = DBI->connect("DBI:mysql:$db:$server", $user,$password); if (! $dbh) { print "Could not connect to $db on $server\n"; } # Prepare and run the query my $query="INSERT INTO clients values( \"$Fname\",\"$Id\") ;"; my $sth = $dbh->prepare_cached("$query") or die "Can't prepare stateme +nt: $dbh->errstr \n"; $sth->execute or die "Can't execute statement", $dbh->errstr ; $sth->finish; $dbh->disconnect(); } # End int();
I have the following setup
Apache 1.3.23
Mod_perl 1.26
Apache::DBI 0.88
AuthMySQL 2.20
Perl 5.6.0
MySQL 3.23.49

Thanks Paul

Replies are listed 'Best First'.
Re: Printing DBI errors from a CGI script
by perlplexer (Hermit) on Apr 18, 2002 at 20:04 UTC
    You have RaiseHell set to 1, which means that DBI methods will die on you and print error messages on the screen (where available).
    Well, guess what? Your browser will surely complain if your script sends an error message instead of the expected HTTP header.

    You either need to print the HTTP header first
    print header; #... do other stuff
    or do this
    use CGI::Carp qw(fatalsToBrowser);

    --perlplexer

      ... or simply surround the DBI calls with eval()..

      my $dbh; eval { ## bunch of dbi calls.. $dbh = DBI->connect( ... ); $dbh->do( blah ); $dbh->commit; $dbh->disconnect; }; if( my $err = $@ ) { eval{ $dbh->rollback }; eval{ $dbh->disconnect }; ## print header, html, and $err exit 1; # or return, whatever }
Re: Printing DBI errors from a CGI script
by tradez (Pilgrim) on Apr 18, 2002 at 20:01 UTC
    Well if you are going to want error reporting to the browser you are going to have a setup an error function and have the 'or' clause or the if(!dbh){} checking point to that. Something like this might work
    sub errorHandling { my ($error) = @{{@_}}{qw/error}; print "<html> <head> <title> Error </title> </head> <body> You have the following error <br> $error<br> </body> </html>"; }


    Tradez
    "Never underestimate the predicability of stupidity"
    - Bullet Tooth Tony, Snatch (2001)
Re: Printing DBI errors from a CGI script
by Ryszard (Priest) on Apr 19, 2002 at 05:24 UTC
    I've done lots of web, db programming and have found the method that works best for me is to tail the error_log.

    Other than that, there is always the fatalsToBrowser thingy or perhaps creating a __DIE__ handler that will print something nice to the browser for you.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-24 07:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found