Default error handling mode
1,PDO::ERRMODE_SILENT
This is the default mode. PDO will set simple error codes on the statement and database objects. You can use the PDO->errorCode() and PDO->errorInfo() methods to check for errors;
If the error occurs when calling the statement object As a result, you can use the PDOStatement->errorCode() or PDOStatement->errorInfo() method on that object to obtain the error information. ## # If the error is caused when calling the database object, you should call those two methods on the database object.
The code is as follows:
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/3/5 0005 * Time: 上午 9:23 */ header("Content-Type:text/html;charset=utf-8"); //mysql:host:localhost;port=3306;dbname=php;charset=utf-8 $dbms='mysql'; $host='localhost'; $port='3306'; $dbname='php'; $charset='utf-8'; //用户名与密码 $user='root'; $pwd='root'; $dsn="$dbms:host=$host;port=$port;dbname=$dbname;charset=$charset"; try{ $pdo=new PDO($dsn,$user,$pwd); //设置错误处理 $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_SILENT); //0 默认模式 // $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_WARNING); //1 // $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);//2 // $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ATTR_ERRMODE); //3 //预处理sql语句 $stmt=$pdo->prepare("select *from phpbook"); $stmt->execute(); //获取错误信息 $code=$stmt->errorCode(); $info=$stmt->errorInfo(); //输出相关信息 print_r("错误码:".$code."<br>"); print_r("错误信息:"); print_r($info); }catch (PDOException $exception){ echo $exception->getMessage().'<br>'; }
If the database is not created, the execution result of the phpbook table is as follows: