Ole K
Ole K

Reputation: 869

How to receive MYSQL warnings (E.g. #1264 Out of Range) from PHP PDO

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

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

Example:

update <table> SET number = 99999999999

should cause MYSQL to throw the below warning when number is an int(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

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

Thank you in advance, Ole

Upvotes: 0

Views: 282

Answers (1)

Ole K
Ole K

Reputation: 869

Thanks to Vicky,

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

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 
// }

Upvotes: 2

Related Questions