close
EN English
Version: v2.3.0

Errors and Error Handling

PDO provides three error handling modes that you can configure on the connection. Read more at https://www.php.net/manual/en/pdo.error-handling.php.
$database = new Medoo([
	// Required.
	'type' => 'mysql',
	'host' => 'localhost',
	'database' => 'name',
	'username' => 'your_username',
	'password' => 'your_password',

	// Optional.
	// PDO::ERRMODE_SILENT (default) | PDO::ERRMODE_WARNING | PDO::ERRMODE_EXCEPTION
	'error' => PDO::ERRMODE_SILENT,
]);

PDO::ERRMODE_SILENT (default)

In this mode, PDO sets error codes silently. You can read the error message from $database->error and detailed error information from $database->errorInfo.
$database = new Medoo([
	// ...
	'error' => PDO::ERRMODE_SILENT
]);

$database->select("bccount", "*");

var_dump($database->error);
var_dump($database->errorInfo);

// string(36) "Table 'master.bccount' doesn't exist"
//
// array(3) {
//   [0]=> string(5) "42S02"
//   [1]=> int(1146)
//   [2]=> string(36) "Table 'master.bccount' doesn't exist"
// }

PDO::ERRMODE_WARNING

PDO emits a traditional E_WARNING message. This is useful during development because execution continues. You can still read errors through $database->error, just like in PDO::ERRMODE_SILENT.
$database = new Medoo([
	// ...
	'error' => PDO::ERRMODE_WARNING
]);

$database->select("bccount", "*");

var_dump($database->error);
var_dump($database->errorInfo);

// Warning:  PDOStatement::execute(): SQLSTATE[42S02]: Base table or view not found:
// 1146 Table 'bccount' doesn't exist in /src/Medoo.php on line 10
//
// string(36) "Table 'master.bccount' doesn't exist"
//
// array(3) {
//   [0]=> string(5) "42S02"
//   [1]=> int(1146)
//   [2]=> string(36) "Table 'master.bccount' doesn't exist"
// }

PDO::ERRMODE_EXCEPTION

PDO throws a PDOException and stops subsequent execution, which helps you identify failing code paths quickly.
$database = new Medoo([
	// ...
	'error' => PDO::ERRMODE_EXCEPTION
]);

$database->select("bccount", "*");

// This line will not execute.
var_dump($database->error);

// Fatal error:  Uncaught PDOException: SQLSTATE[42S02]: Base table or view not found
// 1146 Table 'master.bccount' doesn't exist in /src/Medoo.php:10
// Stack trace:
// #0 /src/Medoo.php(564): PDOStatement->execute()
// #1 /src/Medoo.php(1652): Medoo\Medoo->exec(Object(PDOStatement), Array)
// #2 /var/www/playground/index.php(44): Medoo\Medoo->select('bccount', Array, Array)
// #3 {main}
// thrown in /src/Medoo.php line 10
// Use try-catch to handle and print the exception message.
try {
	$database->select("bccount", "*");
} catch (PDOException $e) {
	echo $e->getMessage();
}

// SQLSTATE[42S02]: Base table or view not found: 1146 Table 'master.bccount' doesn't exist

Checking Error

In PDO::ERRMODE_SILENT and PDO::ERRMODE_WARNING modes, $database->error and $database->errorInfo are null when no error occurs. You can check for null to determine whether an error happened.
$database->select("bccount", "*");

if ($database->error) {
	echo "Error happened!";
}

// Error happened!
$database->select("account", "*");

if (!$database->error) {
    echo "No error.";
}

// No error.