What is the difference between pdo::errmode_exception pdo::errmode_warning?
How do I look at it, I think that the two other than the format of the feedback information, the other looks the same?
is to explain when there is a "throw" the word, who can simply tell me what the difference?
Reply content:
What is the difference between pdo::errmode_exception pdo::errmode_warning?
How do I look at it, I think that the two other than the format of the feedback information, the other looks the same?
is to explain when there is a "throw" the word, who can simply tell me what the difference?
Error mode Description:
This pattern needs to be used in conjunction with try:
Once an error occurs, it will:
Create an object, $e can also be casually named, from the $e->getmessage () get error information;
Take action;
try{ 要执行的命令...}catch(PDOException $e){ echo "执行命令失败:".$e->getMessage(); 失败后的动作...}
This is actually equal 缺省模式 toif($pdo->errorInfo()[2]) ...
Why use try when creating PDO?
Because at this time even the PDO object does not have, so can not set the error mode, so use try,
Once the PDO is created successfully, the error mode becomes the 缺省模式 same and cannot be used in try.
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
This error, will give $pdo->errorCode() and $pdo->errorInfo()[2] assign value;
You can use if($pdo->errorInfo()[2]) and if($pdo->errorCode()) judge if the script is wrong.
If there is no error, the above is empty;
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
This will echo out an error message, disrupting the script;
Learn to use better when debugging;
When you really use it, there are times when you can't tell if the script is wrong.
Please note the PHP error level article.
WarningLevel errors, which are displayed directly on the page, and cannot be handled by the error handling related business logic.
Exceptionis to throw an object that can be captured Exception and get detailed exception information.
try { $stmt = $pdo->prepare("select * from id = :id"); // PGSQL严格限制数据类型,serial字段使用字符串字段进行匹配将会出错 $stmt->bindValue(':id','string',PDO::PARAM_STR); $stmt->execute(); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);} catch (PDOException $e) { // PDO执行出错时执行 // 可以捕获到PDO抛出的异常,可以获取到详细的错误代码,错误描述,哪个文件第几行出错,本次请求中所有执行的文件等等 var_dump($e);}