PDO::ERRMODE_WARNING
In addition to setting the error code, PDO will issue a traditional E_WARNING message. This setting is useful when debugging and debugging if you just want to see what went wrong without interrupting the flow of the program.
Change the error handling mode of the code in the previous section to PDO::ERRMODE_WARNING:
The complete 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("insert into book(name,author)values(?,?)"); $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>'; }
The running results are as follows:
Next Section