Database : Errors

Running queries sometimes hit a dead end which may be due to different reasons such as:

Database connection error
Database environment error
Database sql error

In order to prevent end users from encountering this errors, developer must be able to handle such errors. By default, all errors have been softened out. This means that when errors occur, the Database class does not by default output those errors. Instead, developer must use predefined methods to handle such errors. Spoova has created two basic methods to test and check errors for any error. By default, spoova also stores its last database error in an environment that is accessable at any level of development. We shall be looking at few ways by which we can handle our database errors

  • Handling connection errors
    The two methods used for handling errors are error_exists() and error(). Whilst error_exists() checks for error, error() returns the error itself.

    Example 1 : Handling connection errors
      $db = ($dbc = new DB())->openDB();
      
      if( $db ) {
      
          echo 'Database connected successfully';
      
      } else {
      
          echo $dbc->error();
      
      }
                            


    In Example 1 above, when a connection is successful, openDB() method returns a DBHandler class else if not successful, it returns an empty value. If an empty value is returned, then an error must have occured. In order to handle that error, we have to call the error() method from the class itself which returns the last occured error.

  • Handling environment errors
    A database may be connected but no database name selected. To work on a specific database, the name must be selected or specified. We can check if a table is selected through of active() method while currentDB() returns the currently selected database name.

    Example 2 : Handling environment errors
      $db = ($dbc = new DB)->openDB();
      
      if($dbc->active()) {
      
          // output the current database selected 
          echo $dbc->currentDB();
      
      } else if( $dbc->error() ) {
          
          // some error occured
          echo $dbc->error();
      
      } else {
      
          // No attempt to connect to database yet!
          
      }
                            


    The method above simply checks if a database is selected and prints out the database name using currentDB(). However, if it does not exist, if an error occured (due to a connection error), then the last error should be displayed using the error() method. The beauty of this approach is that no error gets printed if a connection has not been previously attempted. It is however important to note that when a default connection e.g dbconfig.php is set, this is assumed to be a previous connection.

  • Handling sql errors
    Sql errors are errors that occur after queries have been attempted for execution. To handle these errors, we use the error_exists() and error() methods just as discussed earlier

    Example 3 : Handling sql errors
      $db = ($dbc = new DB)->openDB();
    
      if($db) {
    
        // handler connected : run sql 
        $db->query('select * from users')->read();
    
        if( $results = $db->results() ) {
            
            var_dump( $results );
    
        } else if ( $db->error_exists() ) {
    
            echo $db->error();
    
        }
    
      } else {
    
        // database connection failed
        echo $dbc->error();
    
      }    
                            


    In Example 3, we used our $db to run a query and tested for errors using error_exists and error methods respectively. The error() method can also be used to replace error_exists(). However, using error_exists helps to make our code more readable.

  • Fetching errors globally
    All errors are tracked with the DBStatus::error() which returns the last error encountered when a database operation is performed. This is the global way to fetch an error and it returns any error encountered.

    Example 4 : Fetching errors globally
      $db = ($dbc = new DB)->openDB();
      
      if(DBStatus::error()) {
      
          // output the error 
          echo DBStatus::error();
      
      }
                            

    For any reason, when running queries, if the $db->error() does not return an error, if an error exists, the DBStatus::error() may still be able to find it. However, to ensure that $db->error() always an error if it exists, it has to be set to global mode. This is done by supplying an argument of true to the $db->error() method which will ensure that it remembers to check the DBStatus::error() for any error too. The DBStatus::error() may also be useful when working with database relationships.