PHP Try and Catch for SQL Insert

前端 未结 9 1005
南方客
南方客 2020-12-15 18:05

I have a page on my website (high traffic) that does an insert on every page load.

I am curious of the fastest and safest way to (catch an error) and continue if the

9条回答
  •  难免孤独
    2020-12-15 18:23

    Use any method described in the previous post to somehow catch the mysql error.
    Most common is:

    $res = mysql_query('bla');
    if ($res===false) {
      //error
      die();
    }
    //normal page
    

    This would also work:

    function error() {
      //error
      die()
    }
    $res = mysql_query('bla') or error();
    //normal page
    

    try { ... } catch {Exception $e) { .... } will not work!

    Note: Not directly related to you question but I think it would much more better if you display something usefull to the user. I would never revisit a website that just displays a blank screen or any mysterious error message.

提交回复
热议问题