Checking to see if a call to the DB worked

Discussion in 'Perl' started by Stymiee, Nov 29, 2003.

  1. Stymiee

    Stymiee Guest

    I'm writing a perl script to send out a joke of the day for my website.
    It is accessing a mysql db (I use php for the rest of the site. This will
    be a cron job so I figured perl would be better suited for that). I can
    get it to do everything it is supposed to do to send a the joke via
    email. But what I'm trying to do now is add some error checking so if
    there is a problem, I can be notified about it.

    What I need is to know how to check if a query was succcessful or not.
    For example:

    Code:

    sub changeCurrentJOTD(){
    $dbh = DBI->connect("DBI:mysql:database=$serverDb;host=
    $serverName;port=$serverPort",$serverUser,$serverPass);
    $dbh->do("UPDATE jokes_content_test SET jotd='Yes' WHERE
    jotd='today'");
    $dbh->disconnect;
    }


    How do I check to see programatically if this was successful or not?
     
    Stymiee, Nov 29, 2003
    #1
    1. Advertising

  2. Stymiee

    Nick Santos Guest

    change your do code to include "or &sqlerror" or some other error handling
    subroutine like so:

    $dbh->do("UPDATE jokes_content_test SET jotd='Yes' WHERE
    jotd='today'") or &sqlerror;

    #if it doesn't do it, do the subroutine sqlerror

    sub sqlerror{
    $dead = $dbh->err();
    # err() returns the sql error
    number in my sql. You can then use
    # if statements to handle the
    errors you think will occur
    # if you don't know what the errors
    are, then try to force some
    # and use
    $deadstr=$dbh->errstr();
    # which will then give you
    information in a sentence about the error that occurred
    # so in the future you can do
    better error handling and let it recover from the error
    # once you know it occurred
    print "error occurred - error number $dead - $deadstr";
    # and maybe
    die;
    }

    I hope I've been helpful and told you what you wanted to know
    -Nick

    "Stymiee" <> wrote in message
    news:Xns94429A78C9B7Fstymiee@216.196.97.136...
    > I'm writing a perl script to send out a joke of the day for my website.
    > It is accessing a mysql db (I use php for the rest of the site. This will
    > be a cron job so I figured perl would be better suited for that). I can
    > get it to do everything it is supposed to do to send a the joke via
    > email. But what I'm trying to do now is add some error checking so if
    > there is a problem, I can be notified about it.
    >
    > What I need is to know how to check if a query was succcessful or not.
    > For example:
    >
    > Code:
    >
    > sub changeCurrentJOTD(){
    > $dbh = DBI->connect("DBI:mysql:database=$serverDb;host=
    > $serverName;port=$serverPort",$serverUser,$serverPass);
    > $dbh->do("UPDATE jokes_content_test SET jotd='Yes' WHERE
    > jotd='today'");
    > $dbh->disconnect;
    > }
    >
    >
    > How do I check to see programatically if this was successful or not?
     
    Nick Santos, Nov 30, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U3RldmUgWS4=?=

    FIX: Creating a New Web Project Error - This worked for me

    =?Utf-8?B?U3RldmUgWS4=?=, Apr 19, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    273
    =?Utf-8?B?U3RldmUgWS4=?=
    Apr 19, 2004
  2. =?Utf-8?B?RGFuIE5hc2g=?=

    setting page title worked, now doesnt?

    =?Utf-8?B?RGFuIE5hc2g=?=, Oct 14, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    363
    =?Utf-8?B?RGFuIE5hc2g=?=
    Oct 14, 2004
  3. Flip
    Replies:
    3
    Views:
    979
    Tony Morris
    Feb 9, 2004
  4. Steve Kershaw
    Replies:
    1
    Views:
    364
    Brennan Stehling
    Sep 26, 2006
  5. Blubaugh, David A.
    Replies:
    0
    Views:
    276
    Blubaugh, David A.
    Sep 11, 2008
Loading...

Share This Page