如何从PHP PDO接收MYSQL警告(例如#1264超出范围)

时间:2021-07-23 16:42:10

while I am working with PHP PDO I noticed that I can change the ERROR MODE to Exceptions.

当我使用PHP PDO时,我注意到我可以将ERROR MODE更改为Exceptions。

But what if I need to know the MYSQL WARNINGS as well.

但是如果我还需要知道MYSQL警告怎么办呢。

Example:

update <table> SET number = 99999999999

should cause MYSQL to throw the below warning when number is an int(11)

当number为int时,应该导致MYSQL抛出以下警告(11)

Warning: #1264 Out of range value for column 'number' at row 1

To keep my data consistent between the application and MYSQL I would like to catch it

为了保持应用程序和MYSQL之间的数据一致,我想抓住它

A possible workaround is to validate the input through PHP. But thats not what I am searching for.

可能的解决方法是通过PHP验证输入。但这不是我要找的东西。

Thank you in advance, Ole

Ole先生,谢谢你

1 个解决方案

#1


Thanks to Vicky,

感谢Vicky,

possible solutions is to do a MYSQL query on "SHOW WARNINGS"

可能的解决方案是在“SHOW WARNINGS”上进行MYSQL查询

Example:

$warnings = $this->PDO->query("SHOW WARNINGS")->fetchObject();
// example output of $warnings OR NULL
// stdClass Object
// (
//        [Level] => Warning
//        [Code] => 1264
//        [Message] => Out of range value for column 'qty' at row 1 
// }

#1


Thanks to Vicky,

感谢Vicky,

possible solutions is to do a MYSQL query on "SHOW WARNINGS"

可能的解决方案是在“SHOW WARNINGS”上进行MYSQL查询

Example:

$warnings = $this->PDO->query("SHOW WARNINGS")->fetchObject();
// example output of $warnings OR NULL
// stdClass Object
// (
//        [Level] => Warning
//        [Code] => 1264
//        [Message] => Out of range value for column 'qty' at row 1 
// }