读取int或float时,PDO SQLSRV和PDO MySQL返回字符串 [英] PDO SQLSRV and PDO MySQL return strings when fetching int or float

查看:105
本文介绍了读取int或float时,PDO SQLSRV和PDO MySQL返回字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PDO MS SQL Server和PDO MySQL都在获取时返回字符串数组,即使列的SQL类型是数字类型(例如int或float)也是如此.我设法解决了这个问题,但是我想了解为什么要以这种方式设计它们.是因为PDO本身不支持(或以前不支持)吗?

PDO MS SQL Server and PDO MySQL both return an array of strings when you fetch, even if the SQL type of the columns were meant to be numeric types such as int or float. I was managed to work around it, but I want to understand why they were designed this way to begin with. Is it because PDO itself don't support it (or didn't support it before)?

推荐答案

使用PDO MySQL 调用mysql_fetch_row()时的字符串结构.由于PHP是一种松散类型的语言,可根据需要自动转换为整数,因此我怀疑PDO开发人员选择按原样返回它们.这比查找每种列类型并动态转换为整数/浮点数要快.

With PDO MySQL the underlying C API returns a structure of strings when calling mysql_fetch_row(). Since PHP is a loosely typed language that automatically casts to integer as necessary, I suspect the PDO developers chose to return them as-is. This would be faster than looking up each column type and dynamically casting to an integer / float.

专门针对浮点数,本地PHP浮点数与从字符串转换后的原始存储值可能会有所不同.例如,MySQL列支持精度,而PHP浮点数不支持.将浮点数作为字符串从数据库中保留下来可以让您进行更多控制(例如,选择使用一个库(具有不同的精度).

Specifically regarding float, a native PHP float may be different than the original stored value when converted from a string. For example, MySQL columns support a precision while PHP floats do not. Leaving floats as a string from the database allows you more control (e.g. choosing to use a library with different precision).

这篇关于读取int或float时,PDO SQLSRV和PDO MySQL返回字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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