What is the difference between PDO in mysql-php, Errmode_exception and errmode_warning?

Source: Internet
Author: User
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:

    • Exception mode:
      $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

This pattern needs to be used in conjunction with try:
Once an error occurs, it will:

    1. Create an object, $e can also be casually named, from the $e->getmessage () get error information;

    2. 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.

    • Default mode:

$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;

    • Warning Mode:

$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);}
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.