PHP PDO Errors and Error Handling
  • PDO::ERRMODE_SILENT

This is the default mode. The PDO will simply set the error code and can usePDO::errorCode() and PDO::errorInfo() methods to check statements and database objects. If the error was generated by a call to the statement object, then the PDOStatement::errorCode() or PDOStatement::errorInfo() method on that object can be called. If the error was generated by a call to a database object, then you can call both of these methods on the database object.

  • PDO::ERRMODE_WARNING

In addition to setting the error code, PDO will also issue a traditional E_WARNING message. This setting is useful during debugging/testing if you just want to see what went wrong without interrupting the flow of the application.

  • PDO::ERRMODE_EXCEPTION

In addition to setting the error code, PDO will also throw a PDOException exception class and set its properties to reflect the error code and error message. This setting is also very useful during debugging, as it effectively zooms in on the point in the script where the error occurs, making it possible to very quickly point out potentially problematic areas of the code (remember: if an exception causes the script to terminate, the transaction is automatically returned roll).

Another very useful thing about exception mode is that you can build your own error handling more clearly than traditional PHP-style warnings, and the code that exception mode requires than silent mode and explicitly checking the return value of each database call / less nesting.

Create a PDO instance and set the error mode

<?php
$dsn = "mysql:dbname=testdb;host=127.0.0.1";
$user = "dbuser";
$password = "dbpass";
try {
  $dbh = new PDO($dsn, $user, $password);
  $dbh- setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
  echo "Connection failed: " . $e- getMessage();
}
? 

note:PDO::__construct() will always throw a PDOException if the connection fails, regardless of whether PDO::ATTR_ERRMODE is currently set. Uncaught exceptions are fatal.

Create a PDO instance and set the error mode in the constructor

<?php
$dsn = "mysql:dbname=test;host=127.0.0.1";
$user = "googleguy";
$password = "googleguy";
/*
  Using try/catch around the constructor still works, even if ERRMODE is set to WARNING.
  This is because PDO::__construct will always throw a PDOException if the connection fails.
*/
try {
  $dbh = new PDO($dsn, $user, $password, array(PDO::ATTR_ERRMODE =  PDO::ERRMODE_WARNING));
} catch (PDOException $e) {
  echo "Connection failed: " . $e- getMessage();
  exit;
}
// This will cause the PDO to throw an E_WARNING level error instead of an exception (when the data table does not exist)
$dbh- query("SELECT wrongcolumn FROM wrongtable");
? 

The above routine will output:

Warning: PDO::query(): SQLSTATE[42S02]: Base table or view not found: 1146 Table ‘test.wrongtable’ doesn’t exist in /tmp/pdo_test.php on line 18 add a note add a note

Summary

The above is the whole content of this article. I hope the content of this article has certain reference and learning value for your study or work. Thank you for your support to ZaLou.Cn. If you want to know more about it, please check the related links below

Related articles

Install LAMP

Different layers of the technology stack can be replaced to form different variants LAPP (replace MySQL with PostgreSQL) LAMP (last two letters mean Middleware and PostgreSQL) LNMP or LEMP (Nginx instead of Apache) WAMP (replacing Linux with Microsoft Win

Install Drupal

Preface Drupal is an open source CMS software implemented in php Drupal is open-source (free) content-management framework , and you can use it to build a wide range of web applications, from basic websites to elaborate API driven monoliths. Because of