尝试消除这个错误一段时间后,我直接测试了 sql 查询:
UPDATE noticias SET
data_destaque_ini='2013-12-03', data_destaque_fim='',
fotoDestaque_x=-3, fotoDestaque_y=-102,
fotoDestaque_escala=154, destacar=1
WHERE idNoticia=3
但是 fotoDestaque_escala
保持与以前相同的值:127。嗯,这是一个“整数”,对吧?所以即使没有任何 sql 错误消息,我打开结构就是这样:列设置为 TINYINT
(-127 到 127),并将其更改为 SMALLINT
解决了问题。
但是考虑到 future ,我对 MySQL 的这种行为感到震惊:我为该列传递了一个太大的值,MySQL 保存了可能的值,削减了其余部分,并且什么也没说 - 客户端仍然存在在黑暗中!
那么,有没有办法以某种方式配置或检测溢出?如果没有,我正在考虑在我的库中进行 php 级别测试...
最佳答案
直接取自11.2.6 Out-of-Range and Overflow Handling在 MySQL 文档中:
When MySQL stores a value in a numeric column that is outside the permissible range of the column data type, the result depends on the SQL mode in effect at the time:
- If strict SQL mode is enabled, MySQL rejects the out-of-range value with an error, and the insert fails, in accordance with the SQL standard.
- If no restrictive modes are enabled, MySQL clips the value to the appropriate endpoint of the range and stores the resulting value instead.
所以如果你想要出错,你需要将MySQL 置于严格SQL 模式。 5.1.7 Server SQL Modes 中描述了如何更改模式.
关于php - 有没有办法检测 mysql 中的溢出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22048754/