Get Started

Upgrade

Where Syntax

Query

Aggregation

Fetch

Transaction

Management

Advanced

Raw object

PDO object

Debug

Information

version: 2.1.12

Errors and error handling

There are three different error handling strategies on PDO that you can set up on the connection. Read more from 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 will simply set the error code for you, and you can read the error message directly from $database->error, or read error detailed 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 will emit a traditional E_WARNING message. It\'s useful for debugging/testing on development without interrupting the flow of the application. You can still able to read the error message like PDO::ERRMODE_SILENT via $database->error.

$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 will throw a PDOException, and all following codes will be terminated, quickly pointing the finger at potential problem areas in your code.

$database = new Medoo([
	// ...
	'error' => PDO::ERRMODE_EXCEPTION
]);

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

// It will not call this.
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
// You can use try and catch to output the 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

On PDO::ERRMODE_SILENT and PDO::ERRMODE_WARNING mode, $database->error and $database->errorInfo will be null value if no error occurred. You can simply check if the value is null or not to know about that.

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

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

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

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

// No error.