MeekroDB Quick Start Docs FAQ
Download GitHub

Error (and Success) Handlers

If a query returns an error, MeekroDB will normally print out some debugging information to the screen and exit. This behavior reflects the age of this library (first released in 2011) when such things were standard in the PHP world.

Modern PHP relies more heavily on exceptions. You can set MeekroDB to use modern Exception-based error handling like this:

DB::$error_handler = false; // disable standard error handler
DB::$nonsql_error_handler = false; // disable standard error handler
DB::$throw_exception_on_error = true; // throw exception on mysql query errors
DB::$throw_exception_on_nonsql_error = true; // throw exception on library errors (bad syntax, etc)

Alternatively, you can set your own error handlers to show custom errors to the user, write logs to disk, etc.

DB::$error_handler = 'my_error_handler'; // runs on mysql query errors
DB::$nonsql_error_handler = 'my_error_handler'; // runs on library errors (bad syntax, etc)

function my_error_handler($params) {
  echo "Error: " . $params['error'] . "<br>\n";
  echo "Query: " . $params['query'] . "<br>\n";
  die; // don't want to keep going if a query broke
}

// this broken query will cause my_error_handler() to run
DB::query("SELCT * FROM accounts"); // misspelled SELECT

You can also set a success handler to run every time a query succeeds. This is a great way to make a list of the queries your application is running, and the built-in Debug Mode relies on this feature.

DB::$success_handler = 'my_success_handler';
function my_success_handler($params) {
  echo "Command: " . $params['query'] . "<br>\n";
  echo "Time To Run It: " . $params['runtime'] . " (milliseconds)<br>\n";
}
$results = DB::query("SELECT * FROM accounts");