PHP:7 PDO fetch(All)尝试将类型转换为关联的类型 [英] PHP: 7 PDO fetch(All) tries to convert types to associated type

查看:84
本文介绍了PHP:7 PDO fetch(All)尝试将类型转换为关联的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我发现我们的代码似乎与PDO的更改向后不兼容.在PHP< 5.6中,结果从PDOStatement通过其函数fetchfetchAll返回的行的值均为字符串或null.我们的代码依赖于此属性.

前一段时间,我们更新到PHP7.事实证明,PDO现在尝试将 some 数据类型转换为其他关联类型.例如,FLOAT列在PDOStatement结果集中创建一个浮点值. TINYINT列在结果集中创建一个整数值.有趣的是,当BIGINT或较大的INT UNSIGNED无法显示为整数并且某些其他类型(例如DECIMAL)未转换为任何内容时,它会显示为字符串.它们仍然是字符串.我认为这是一个公认的原则,即将数据类型从MySQL数据库转换为PHP数据类型是有问题的,不应该由PHP本身执行,但是显然PHP 7决定这样做.

因此,PHP 7的PDO引入了一组转换规则,该转换规则在内部用于转换从数据库中选择的值,但是这弄糟了我的代码库,并且由于这些转换规则的不一致,我宁愿不更改我的代码基地适合他们.可能有某种设置或标志可以切换以防止PDO转换其获取的值吗?

解决方案

它不是PHP7,而是名为的基础驱动程序mysqlnd .
而且,它不是一组转换规则,而是传输协议的工作方式:当同时使用mysqlnd和本机预先指定的语句时,则使用二进制传输协议,这意味着始终存在有关数据类型的信息.因此,只是将数据从二进制格式解压缩为适当类型的变量-当PHP具有适当的变量时,即INT和FLOAT(请注意,由于这种类型的性质,将返回DECIMAL类型的字符串).

如果您不希望出现这种情况,可以为此使用一个配置选项

$pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);

将这种行为恢复为仅字符串和空值

Today I found out that our code appears to be backward incompatible with a change in PDO. In PHP <5.6 the result sets from a PDOStatement through its functions fetch and fetchAll returned rows whose values where either a string or null. Our code relies on this attribute.

A while ago we updated to PHP 7. It turns out that PDO now attempts to convert some datatypes to other associated types. For example, a FLOAT column creates a float value in the PDOStatement result set. And a TINYINT column creates an integer value in the result set. Interestingly, a BIGINT or a large INT UNSIGNED shows up as a string when it cannot be displayed as an integer and certain other types (such as DECIMAL) are not converted to anything. They remain strings. I thought it was a generally agreed upon principle that converting data types from a MySQL database to PHP data types is problematic and should not be carried out by PHP itself, but apparently PHP 7 decided otherwise.

So PHP 7's PDO introduces a set of conversion rules that it uses internally to convert values selected from a database, but that screws up my code base and because of the inconsistencies of these conversion rules, I'd rather not change my code base to suit them. Is there maybe some sort of setting or flag which I can toggle to prevent PDO from converting the values it fetches?

解决方案

It is not PHP7 but the underlying driver called mysqlnd.
Also, it is not a set of conversion rules but the way the transport protocol works: when both mysqlnd and native prepatred statements are used, then the binary transport protocol is used, means there is always an information about the data type. So the data just gets unpacked from the binary format right into a variable of the proper type - when PHP has an appropriate one, namely INTs and FLOATs (note that for the DECIMAL type string is returned, due to nature of this type).

In case you don't want this behavior, there is a configuration option for this

$pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, true);

will revert this behavior to just strings and nulls as before

这篇关于PHP:7 PDO fetch(All)尝试将类型转换为关联的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆