Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Feb 2006
    Posts
    138
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Error handling for my SQL queries

    I'd like to set up some custom error handling for my php scripts.

    I see the string "or die(mysql_error());" at the end of each of my queries, so I figure there's a way to define a global error handling variable that can get put into this statement, I'm just not sure how to do it..

    I just want to insert a line that tells the user of the error, and provides a link to the original page, so they can try again...

    Any help is greatly appreciated!

  • #2
    New Coder
    Join Date
    Apr 2006
    Location
    UK
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Firstly, what do you mean by 'global error handling variable'?
    To put a link to let them go back to where they were previously, just put:
    PHP Code:
    "<a href=\"previous_page.php\">Back</a>" 
    as part of the parameter for the die()...

  • #3
    Regular Coder
    Join Date
    Feb 2006
    Posts
    138
    Thanks
    11
    Thanked 0 Times in 0 Posts
    I mean defining something like

    Code:
    $error = echo 'Error connecting to the DB, click <a href="http://***>HERE</a> to return to the home page';
    and then using that in this sort of way
    Code:
    or die(mysql_error($error));
    or something to that effect.. Is this possible, and if so, what's the proper syntax?

  • #4
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    PHP Code:
    $rs=mysql_query($query) or die($error); 
    Will do what you're asking for.

  • #5
    Regular Coder
    Join Date
    Feb 2006
    Posts
    138
    Thanks
    11
    Thanked 0 Times in 0 Posts
    So all I have to do is replace the mysql_error() with the $error variable that I defined and I'm good to go?

  • #6
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    yes, although while developing whatever it is you're making, it'd be sensible to keep the mysql_error there, so you've got a better idea of what's happened should thnings go wrong.

    [edit]
    To this effect, you could have something like:
    PHP Code:
    $rs=mysql_query($query) or die(handle_error());

    function 
    handle_error() {
    gloabl $debug;
    echo (
    $debug)?mysql_error():'nice error message with link';


  • #7
    Regular Coder
    Join Date
    Feb 2006
    Posts
    138
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Actually, I'm done debugging it, it's ready to release to the public, I just need some kind of fancy message in case the MySQL DB craps itself one day...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •