How to let php-script continue after MySQL-error

Posted on 2007-08-08
Last Modified: 2013-12-12
I've got a small website made with php and using MySQL for storing information. In the database I've made some relationships between some of the tables.

However, when I try to for instance 'delete from user where userid=2' and userid 2 is used elsewhere, I'm unable to catch the error and give a meaningful error message to the user. What I get is:
ERROR: unable to execute query 'delete from User where UserId='2'' and the php-script is halted.

I've tried going through php.ini, setting error_reporting(0) and
    $result = SQLQuery($query);
    return "User deleted successfully";
  }catch(Exception $e){
    return "Some error message";

I could of course check that the userid is not in use before I try to delete but that defeats some of the purpose of the foreign key contraint.
Question by:johnnybaluba
    LVL 48

    Accepted Solution

    > ERROR: unable to execute query 'delete from User where UserId='2'' and the php-script is halted.
    This message is generated by your application, not php. Seems that the Database layer you are using stops if a SQL-errors occurs and does not throw an execption. You need to change those things in your use database layer, so you can catch that error and give proper error messages.
    LVL 17

    Expert Comment

    Here's what I get when trying to delete a row with a RESTRICT constraint - the execution did not stop and the error seems friendly enough:
    Cannot delete or update a parent row: a foreign key constraint fails

    Here's the code which produced it:

      function sql_exec($aSQL, $return_affected = 0, $return_insert_id = 1) {
        $my_server   = 'localhost';
        $my_username = 'my_user';
        $my_password = 'my_pass';
        $my_database = 'test';

        $aID = false;
        if($aSQL != '') {
          # connect to database...
          $data = mysql_connect($my_server, $my_username, $my_password);
          if(!empty($data)) {
            if(mysql_select_db($my_database)) {
              mysql_query('SET NAMES utf8');
              $theSQL = $aSQL;
              $query = mysql_query($theSQL);
              if($query) {
                  $aID = mysql_affected_rows();
                else if($return_insert_id)
                  $aID = mysql_insert_id();
                  $aID = true;
              else {
                print mysql_error($data) . " <BR>\n";

        return $aID;

      $aSQL = "DELETE FROM myOne WHERE id = 1 ";
      print "BEFORE!<BR>\n";
      print "AFTER!<BR>\n";

    LVL 19

    Expert Comment

    Precede your mysql functions (or any functions for that matter) with an @ symbol to supress errors.

    $query = @mysql_query($theSQL);

    if($query) {

    //proceed with result processing

    } else {

    //output an alternative

    LVL 11

    Expert Comment

    v2Media got a good one.
    Errors should also be caught like hernst42 said.  In php i believe it can be done as in
    Thats a propper way of dealing with errors across all languages.

    Author Comment


    :"> I feel really stupid now. I've made my own DB layer and for some obscure reason I'd put this inside the SQLQuery function:
    if (!$res)
        echo("<B>ERROR:</b> unable to execute query '$query'");

    Why I didn't check there first is beyond me. Anyway, thanks for the tips.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now