The PDO Error Mode in mysql-PHP. What is the difference between ERRMODE_EXCEPTION and ERRMODE_WARNING?

Source: Internet
Author: User
PDO: ERRMODE_EXCEPTIONPDO: ERRMODE_WARNING what is the difference? After reading this, I think the formats of the feedback information are different. What else does it look like? That is, when I explain it, I have a word & quot; throw & quot;. Who can tell me what the difference is? PDO: ERRMODE_EXCEPTION PDO: ERRMODE_WARNING what is the difference?

After reading this, I think the formats of the feedback information are different. What else does it look like?

It is the word "throw" during the explanation. Who can tell me what the difference is?

Reply content:

PDO: ERRMODE_EXCEPTION PDO: ERRMODE_WARNING what is the difference?

After reading this, I think the formats of the feedback information are different. What else does it look like?

It is the word "throw" during the explanation. Who can tell me what the difference is?

Error Mode description:

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

This mode must be used with try:
Once an error occurs, it will:

  1. Create an object, $ e can also be named at will, get error information from $ e-> getMessage;

  2. Action taken;

Try {command to be executed ...} catch (PDOException $ e) {echo "failed to execute the command :". $ e-> getMessage (); action after failure ...}

This is actually equalDefault ModeOfif($pdo->errorInfo()[2]) ...

Why is try used when creating PDO?
Because no PDO object is connected at this time, the error mode cannot be set, so try,
Once the PDO is successfully created, the error mode becomesDefault Mode, You cannot use try.

  • Default Mode:

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);

This error will be returned$pdo->errorCode()And$pdo->errorInfo()[2]Value assignment;

Availableif($pdo->errorInfo()[2])Andif($pdo->errorCode()) Check whether the script has an error.

If no error occurs, the above fields are empty;

  • Warning mode:

$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);

This will echo an error message and disrupt the script;
It is better to use it when learning and debugging;
When the script is used up, it is often unable to determine whether the script has an error;

Pay attention to the article "PHP error level.

WarningLevel errors are directly displayed on the page, and relevant business logic cannot be processed based on the errors.
ExceptionThen, a captured object is thrown.ExceptionObject To obtain detailed exception information.

Try {$ stmt = $ pdo-> prepare ("select * from id =: id"); // PGSQL strictly limits the data type, an error occurs when the serial field matches the string field $ stmt-> bindValue (': id', 'string', PDO: PARAM_STR); $ stmt-> execute (); $ rows = $ stmt-> fetchAll (PDO: FETCH_ASSOC);} catch (PDOException $ e) {// execute when an error occurs during execution of PDO // exceptions thrown by PDO can be captured, detailed error code, error description, line number of the file with errors, all executed files in this request, and so on 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.