#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2004
    Location
    Boston, MA USA
    Posts
    582
    Rep Power
    51

    Graceful Exit after MySQL error


    Hi:

    Is there a way to display a polite error page when MySQL encounters an error. For example, if a "$_GET" variable gets corrupted, a MySQL command may fail and the user sees just a blank white screen that says "You have an error in your SQL syntax..."

    Obviously, it would be best if there never was a MySQL error in the first place, but sometimes it happens.

    Is there a way to have the page diver to a nicely formatted "Oops, we encountered an error..." page?

    Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,473
    Rep Power
    653
    Put the MySQL execute in a try/catch block.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2004
    Location
    Boston, MA USA
    Posts
    582
    Rep Power
    51
    Originally Posted by gw1500se
    Put the MySQL execute in a try/catch block.
    Hmmm... never heard of a "try/catch block"... I will research that...

    Just goes to show... "You don't know what you don't know"
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2017
    Location
    Worldwide
    Posts
    49
    Rep Power
    45
    Originally Posted by gw1500se
    Put the MySQL execute in a try/catch block.
    No, don't do that. You will litter your code base. You can use set_exemption_handler to customize error handling. There is also set_error_handler
    Last edited by benanamen; November 27th, 2018 at 01:28 PM.
  8. #5
  9. Backwards Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,914
    Rep Power
    9646
    First, there is no reason whatsoever that any query should ever fail with a syntax error. If it can then you're Doing It Wrong and likely open to SQL injection.

    If we're talking about an exception raised because the database connection failed or something catastrophic and unlikely, then set_exception_handler is fine. All that function does is give you a last chance to display something nice to the user. You cannot correct the action and try again. You can't even prevent PHP from aborting. All you can do is try to give the user a friendly error page.

    If we're talking about exceptions that aren't catastrophic then a try/catch block is most definitely the correct answer.

IMN logo majestic logo threadwatch logo seochat tools logo