Detailed explanation of php PDO exception handling method

墨辰丷
Release: 2023-03-28 14:14:01
Original
1660 people have browsed it

This article mainly introduces you to the relevant content of PDO exception handling in php. The article first gives you a brief introduction, and then introduces how to use it through example code. The introduction is very detailed, friends in need We can refer to it. Friends who are interested can come and learn together.

Exception handling:

PHP: The default is to report errors directly

MYSQL: The default is silent mode, if you make an error, you will make an error. No error reporting

PDO: The default is silent mode, errors are errors, no errors are reported

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

pdo exception handling settings:

Set PDO::ATTR_ERRMODE, which has the following three values:

PDO::ERRMODE_SILENT: Default mode, does not actively report errors, you need to actively use $pdo->errorInfo () to obtain error information.

PDO::ERRMODE_WARNING: Raise an E_WARNING error and actively report errors

PDO::ERRMODE_EXCEPTION: Actively throw exceptions, and you need to output error information with try{}cath(){}.

Example:

Method 1:

<?php
//默认是不提示的 需要用 errorCode() errorInfo();
try{
  $pdo = new PDO("mysql:host=localhost;dbname=jikexueyuan","root","");
  //下面这句是默认设置,有无均可
  $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_SILENT);
}catch(PDOException $e){
  die("数据库连接失败".$e->getMessage());
}
$sql = "insert into stuu values(null,&#39;jike&#39;,&#39;w&#39;,55)";
$res = $pdo->exec($sql);
if($res){
  echo "OK";
}else{
  echo $pdo->errorCode();
  print_r($pdo->errorInfo());
}
Copy after login

Method two:

<?php
try{
  $pdo = new PDO("mysql:host=localhost;dbname=jikexueyuan","root","");
  //主动以警告的形式报错
  $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
}catch(PDOException $e){
  die("数据库连接失败".$e->getMessage());
}
$sql = "insert into stuu values(null,&#39;jike&#39;,&#39;w&#39;,55)";
//若有错误直接在浏览器页面中显示错误信息
$res = $pdo->exec($sql);
Copy after login

##Method three:

<?php
try{
  $pdo = new PDO("mysql:host=localhost;dbname=jikexueyuan","root","");
  //主动抛出异常
  $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION );
}catch(PDOException $e){
  die("数据库连接失败".$e->getMessage());
}
$sql = "insert into stuu values(null,&#39;jike&#39;,&#39;w&#39;,55)";
//用try{}catch(){}抓取异常
try{
  $res = $pdo->exec($sql);
}catch(PDOException $e){
  echo $e->getMessage();
}
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

Detailed explanation of how to query the location of a mobile phone using PHP

Detailed graphic and text explanation of common solutions to Notice errors in PHP

PHP realizes conversion between ASCII code and string

The above is the detailed content of Detailed explanation of php PDO exception handling method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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