errorInfo()"; 2. Set "PDO::ERRMODE_WARNING" and check the "E_WARNING" error; 3. Set "PDO::ERRMODE_EXCEPTION" and then output the error message with "try{}cath(){}"."/> errorInfo()"; 2. Set "PDO::ERRMODE_WARNING" and check the "E_WARNING" error; 3. Set "PDO::ERRMODE_EXCEPTION" and then output the error message with "try{}cath(){}".">

Home  >  Article  >  Backend Development  >  How to set up php pdo error handling

How to set up php pdo error handling

藏色散人
藏色散人Original
2022-10-19 09:59:181393browse

php pdo error handling setting method: 1. Set "PDO::ERRMODE_SILENT", and then obtain the error information in the form of "$pdo->errorInfo()"; 2. Set "PDO::ERRMODE_WARNING" , check the "E_WARNING" error; 3. Set "PDO::ERRMODE_EXCEPTION", and then output the error message with "try{}cath(){}".

How to set up php pdo error handling

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

php PDO exception handling details

Exception handling:

PHP: The default is to report errors directly

MYSQL: Default It is the silent mode. If an error occurs, it will be an error. No error will be reported.

PDO: The default is silent mode. If an error occurs, the error will be reported. No error will be reported.

In the past, when PHP encountered an error, it would directly report an error. It can become quite cumbersome to deal with. 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, there are The following three values:

PDO::ERRMODE_SILENT: The default mode does not actively report errors. You need to actively obtain error information in the form of $pdo->errorInfo().

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

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

Example:

Method one:

setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_SILENT);
}catch(PDOException $e){
  die("数据库连接失败".$e->getMessage());
}
$sql = "insert into stuu values(null,'jike','w',55)";
$res = $pdo->exec($sql);
if($res){
  echo "OK";
}else{
  echo $pdo->errorCode();
  print_r($pdo->errorInfo());
}

Method two:

setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING);
}catch(PDOException $e){
  die("数据库连接失败".$e->getMessage());
}
$sql = "insert into stuu values(null,'jike','w',55)";
//若有错误直接在浏览器页面中显示错误信息
$res = $pdo->exec($sql);

Method three:

setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION );
}catch(PDOException $e){
  die("数据库连接失败".$e->getMessage());
}
$sql = "insert into stuu values(null,'jike','w',55)";
//用try{}catch(){}抓取异常
try{
  $res = $pdo->exec($sql);
}catch(PDOException $e){
  echo $e->getMessage();
}

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to set up php pdo error handling. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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