http://www.perlmonks.org?node_id=21734

derek has asked for the wisdom of the Perl Monks concerning the following question: (database programming)

I am trying to use MySQL through Perl (but this has also happened doing other things). I can print before I connect, but afterwards it won't print. I cann't be sure if it is connecting or not because I can't get any print out to see. Below, I get print 1 and 2, but not 3. Is this Perl or some server problem?
#!/usr/bin/perl print "Content-type: text/html\n\n"; print "OK 1"; use DBI; print "OK 2"; $dbh = DBI->connect("DBI:mysql... $dbh->disconnect(); print "OK 3";

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How can I see error messages from MySQL?
by autark (Friar) on Jul 10, 2000 at 04:27 UTC
    The fact that it doesn't print "OK 3" suggests that something went wrong inbetween "OK 2" and "OK 3". You don't see the error message, because it's printed to STDERR, and the HTTP-server doesn't catch that output. Try to put this in your script: use CGI::Carp qw(fatalsToBrowser); This will ensure that messages to STDERR will be displayed in your browser so that you easier can diagnose the problem. Autark
Re: How can I see error messages from MySQL?
by BBQ (Deacon) on Jul 10, 2000 at 09:59 UTC
    DBI has return values for each one of its calls, so it would be wise to or your connect statement, like:
    $dbh = DBI->connect("DBI::mysql ... etc $dsn") or die("Couldn't connect to $dsn: $DBI::errstr\n");
    this way, you'll always get an error message if something goes wrong.
Re: How can I see error messages from MySQL?
by btrott (Parson) on Jul 10, 2000 at 10:28 UTC
    To follow up with what BBQ wrote... you can either check the return value of your database functions, or you can just use the RaiseError attribute, which will die on an error:
    my $dbh = DBI->connect($dsn, $user, $pass, 'mysql', { RaiseError = +> 1 });
Re: How can I see error messages from MySQL?
by PsychoSpunk (Hermit) on Jul 12, 2000 at 20:20 UTC
    To continue the follow up to the previous follow up... if you RaiseError, you may find it handy to use the variable for the error string produced by the DBMS irregardless of which one you choose. This of course is $DBI::errstr which will return the ever cryptic messages that seem to be returned from every DBMS I've used.
Re: How can I see error messages from MySQL?
by pmas (Hermit) on Jul 27, 2001 at 20:39 UTC
    DBI has excellent function DBI->trace(2), printing a string for each DBI action made (as connect, prepare, execute). Will even show you which data were fetched, row by row. Excellent for debug.