本文介绍了为什么PDO_MySQL不返回整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的PHP代码从mysql(在5.5中弃用)迁移到PDO_MySQL.但是,mysql_fetch_row返回整数,而PDOStatement::fetch返回数字字符串.如何使PDO表现得像前者一样?

I'm migrating my PHP codes from mysql (deprecated in 5.5) to PDO_MySQL. However, mysql_fetch_row returns integer while PDOStatement::fetch returns strings for numbers. How can I make PDO behaves like the former one?

mysql_fetch_row的结果:

array(1) {
  ["id"]=>
  int(1)
}

PDOStatement::fetch的结果:

array(1) {
  ["id"]=>
  string(1) "1"
}

推荐答案

如果您确实需要使用松散类型的PHP,请将PDO::ATTR_EMULATE_PREPARES设置为false

Set PDO::ATTR_EMULATE_PREPARES to false, if you really need it with loosely-typed PHP

如果mysql_fetch_row要么返回int来求和(我从没想过检查过)-那么它确实具有if (ctype_digit($val)) $row[$key] = (int)$val;这样的魔力-因此您可以在DBAL中做

If mysql_fetch_row returns you int for SUM either (I never cared to check) - then it does some magic like if (ctype_digit($val)) $row[$key] = (int)$val; - so you can do in your DBAL

据我了解,预处理语句的工作方式,它使用相同的数据包结构发送和检索数据,并且此数据包包含数据类型.

As far as I understand the way prepared statements works, it uses the same packet structure for either sending and retrieving data, and this packet contains data type.

服务器可以返回两种格式的数据-本机和mysqlnd,具体取决于请求类型.客户端库可以解释为后者,以转换结果值.

It looks like that server can return data in 2 formats - native and mysqlnd, depends on the request type. A latter one can be interpreted by client library to cast resulting value.

这篇关于为什么PDO_MySQL不返回整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 07:08