Analysis of PDO Exception Handling Operation in php Implementation

  • 2021-11-13 06:53:04
  • OfStack

This article illustrates the PDO exception handling operation implemented by php. Share it for your reference, as follows:

Exception handling:

PHP: Direct error reporting by default

MYSQL: Silent mode by default, error is wrong, no error is reported

PDO: Silent mode by default, error is wrong, no error is reported

In the past, when PHP encountered an error, it would report an error directly, and error handling would become quite troublesome. Later, when an error occurs, the error message is no longer directly output and placed in an object of class 1 (PDOException)

To use PDO exception handling, two conditions must be met

1. You need to change the error handling mode to the exception mode


$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);

2. All possible error statements must be placed in the error capture statement block


try{
   // Error capture statement block 
   // All statements that may cause errors are put here 
   //1 Once an error occurs, it will enter immediately catch Statement, put all error messages in the PDOexception $e Inside 
}catch(PDOExecption $e){
   // Handle error messages 
}

For example:


try{
  // Connection authentication 
  $pdo = new PDO('mysql:host=localhost;dbname=project','root','root');
  // Setting Error Handling Mode 
  $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
  // Organization SQL
  $sql= "update pro_student set s_age = 123 where s_id = 20";
  $res = $pdo->exec($sql);
}catch(PDOException $e){
  //var_dump($e);
  // Tell the user where in which file 1 What kind of error occurred in the line 
  echo ' Exception occurred: <br/>';
  echo ' Where the error occurred: ' . $e->getFile() . $e->getLine() . '<br/>';
  echo ' Error cause: ' . $e->getMessage();
  var_dump($e->getTrace());// Get complete error data 
  exit;
}

Sometimes, if the data is executed without any errors, it just does not conform to the specified business logic. 1 When business logic errors occur, there is no way to catch exceptions (exceptions only catch syntax errors). 1 It is generally believed that by judging the execution of statements (results), exceptions are thrown actively, thus ending the operation of error programs.

Syntax: throw new PDOException;


$sql = "select * from pro_student where s_id = 20";
$stmt = $pdo->query($sql);
if($stmt->fetchColumn(4) > 100) throw new PDOException; // Throw an exception and jump to catch Statement block 
else{
  echo ' No problem ';
}

For more readers interested in PHP related contents, please check the topics on this site: "Summary of PHP Database Operation Skills Based on pdo", "Summary of php+Oracle Database Programming Skills", "Encyclopedia of PHP+MongoDB Database Operation Skills", "Introduction to php Object-Oriented Programming", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: