为什么 PDO_MySQL 不返回整数?

2023-05-06php开发问题
4

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

问题描述

限时送ChatGPT账号..

我正在将我的 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"
}

推荐答案

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

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

如果 mysql_fetch_row 为 SUM 返回 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.

看起来那个服务器可以返回两种格式的数据——native 和 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 不返回整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17