Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

How can I see error messages from MySQL?

( #21734=categorized question: print w/ replies, xml ) Need Help??
Contributed by Zombie derek on Jul 10, 2000 at 04:03 UTC
Q&A  > database programming


Description:

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";

Answer: How can I see error messages from MySQL?
contributed by autark

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

Answer: How can I see error messages from MySQL?
contributed by BBQ

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.
Answer: How can I see error messages from MySQL?
contributed by btrott

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 });
Answer: How can I see error messages from MySQL?
contributed by PsychoSpunk

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.

Answer: How can I see error messages from MySQL?
contributed by pmas

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.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (16)
    As of 2014-07-30 08:24 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (229 votes), past polls