Database : Status

Statuses are saved when queries are executed which can be accessed using the DBStatus class. The DBStatus class is the highest level of call as on the DBHandler class. It has the power to modify the messages returned by the DBHandler class. Methods which can be called include :

DBStatus::query()
DBStatus::err()
DBStatus::baseErr()

  • Query
    The query method returns the last executed query

    Example 1
      $db = ($dbc = new DB())->openDB();
      
      if( $db ) {
      
          $db->query('select * from users')->read();
      
          // outputs : select * from users
          echo DBStatus::query() ;
      
      }
                            


    In Example 1 above, when a connection is successful and a query is executed, then the last executed query is displayed.

  • Err (Fetch last error)
    When an error occurs, the last error is saved and can be obtained using the err() method.

    Example 2
      $db = ($dbc = new DB)->openDB();
      
      if( $db ) {
      
          $db->query('select *name from users')->read();
      
          
          if(DBStatus::err()) {
      
              echo DBStatus::err(); // displays error
      
          }
      
      }
                            


    In the Example 2 above, we checked if an error exists in storage and then displayed the error. Athough, this approach is discouraged, it might be useful when working in window classes.

  • Err (Modify error)
    The err() method can also modify an existing error. It takes a string as parameter. The value supplied replaces the last stored error if an error exists. However, if a second parameter of true is defined, then the defined custom error will forcefully overwrite the default response message even if the response is empty.

    using Example 2 above,

    Example 3

      if( DBStatus::err('some custom error') ) {
      
          print DBStatus::err(); //some custom error
      
      }
                            


    In Example 3, if an error occurs from an sql executed query, the DBStatus::err() will replace the error with 'some custom error'. However if no error previously exists, then the custom error will not be set.

    Example 4
      DBStatus::err('some custom error', true);
                            


    In Example 4, setting an argument of true on the DBStatus custom message will force the DBStatus to set a custom error even if no error exists previously

  • baseErr (Default error)
    Since the err() method is capable of modifying the error message returned by the DBStatus class, if an existing error is modified, we can still retrieve the real or default error returned from an sql query by calling the DBStatus::baseErr() method which takes no argument. An example is shown below:

    Example 5

      if( DBStatus::err('some custom error') ) {
      
          print DBStatus::baseErr(); //default error
          print DBStatus::err(); //some custom error
      
      }