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:
This mode must be used with try:
Once an error occurs, it will:
Create an object, $ e can also be named at will, get error information from $ e-> getMessage;
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 Mode
Ofif($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.
$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;
$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.
Warning
Level errors are directly displayed on the page, and relevant business logic cannot be processed based on the errors.
Exception
Then, a captured object is thrown.Exception
Object 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 );}