Home > Backend Development > PHP Tutorial > Recommended articles about PDO::ERRMODE_EXCEPTION

Recommended articles about PDO::ERRMODE_EXCEPTION

黄舟
Release: 2023-03-08 13:02:02
Original
1554 people have browsed it

Use exception mode -PDO::ERRMODE_EXCEPTION (Method 3 for capturing errors in SQL statements in PDO) exception mode will create a PDOException and set the erorCode attribute, which can encapsulate the execution code into a try{...}catch In the {...} statement, uncaught exceptions will cause the script to interrupt and display a stack trace to let the user understand where the problem occurred! In the first two articles "Using the default mode-PDO::ERRMODE_SILENT (Method 1 to capture errors in SQL statements in PDO)" and "Using warning mode-PDO::ERRMODE_WARNING (Method 2 to capture errors in SQL statements in PDO)" We have introduced two methods: default mode and exception mode, so today we will introduce the third method to capture errors in SQL statements in PDO ~ Another very useful thing about exception mode is that it can be clearer than traditional PHP style warnings You can easily build your own error handling, and exception mode requires less code/nesting than silent mode and explicitly checking the return value of each database call. Except

1. Use exception mode-PDO::ERRMODE_EXCEPTION (Capturing errors in SQL statements in PDO three)

Recommended articles about PDO::ERRMODE_EXCEPTION

Introduction: Exception mode creates a PDOException and sets the erorCode attribute, which can encapsulate the execution code into a try{...}catch{...} statement , uncaught exceptions will cause the script to break, and a stack trace will be displayed to let the user understand where the problem occurred!

2. mysql - PDO error mode in PHP, what is the difference between ERRMODE_EXCEPTION and ERRMODE_WARNING?

Introduction: PDO::ERRMODE_EXCEPTION PDO::ERRMODE_WARNING What is the difference? After looking at it, I feel that except for the format of the feedback information, everything else looks the same? There is a word "throw" in the explanation. Who can simply tell me what the difference is?

3. php database pdo connection

Introduction: None No details?php$dbh = new PDO('mysql:host=localhost;dbname=demo2', 'root', '123456'); $dbh-setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $dbh -exec('set names utf8'); ?

[Related Q&A recommendations]:

mysql - Error mode of PDO in PHP, What is the difference between ERRMODE_EXCEPTION and ERRMODE_WARNING?

The above is the detailed content of Recommended articles about PDO::ERRMODE_EXCEPTION. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template