如何使用 PDO 从 MySQL 获取数字类型?

发布于 2024-07-29 06:47:46 字数 249 浏览 4 评论 0原文

我正在使用 PDO 和 MySQL,由于某种原因,当从数据库获取 int 类型的值时,PDOStatement 返回数字的字符串表示形式,而不是数字类型的值。 我该如何防止这种情况发生?

我注意到 PDO 类有一个属性: PDO::ATTR_STRINGIFY_FETCHES 应该处理这个问题,但是当尝试修改它时,它会抛出一个错误,指出该属性对 MySQL 无效司机。

查询数据库时获取字符串而不是数字是否正常?

I'm using PDO and MySQL, for some reason when getting values from the database that are int type, the PDOStatement is returning a string representation of the number and not a value of numeric type. How do I prevent this from happening?

I noticed there is a attribute of the PDO class: PDO::ATTR_STRINGIFY_FETCHES that is supposed to take care of this but, when trying to modify it, it throws an error saying the attribute is not valid for MySQL driver.

Is it normal to get strings instead of numbers when consulting a database?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吃不饱 2024-08-05 06:47:46

首先回答你的最后一个问题,“是的”,不幸的是,接收数字作为字符串是正常的。 正如 Pascal 引用的手册所说,mysqlnd(PHP 5.3)将从准备好的语句返回本机数据类型,前提是您关闭 PDO 的准备好的语句模拟。

new PDO($dsn, $user, $pass, array(
    PDO::ATTR_EMULATE_PREPARES => false
))

PDO::ATTR_STRINGIFY_FETCHES 与 MySQL 无关。

如果你看到好的一面,无论如何使用准备好的语句都是很好的做法,所以......;)

To answer your last question first, "yes," unfortunately it's normal to receive numbers as strings. As the manual quoted by Pascal says, mysqlnd (PHP 5.3) will return native data types from prepared statements, provided you turn off the prepared statement emulation from PDO.

new PDO($dsn, $user, $pass, array(
    PDO::ATTR_EMULATE_PREPARES => false
))

PDO::ATTR_STRINGIFY_FETCHES is unrelated to MySQL.

If you look at the bright side, it's good practice to use prepared statements anyway, so... ;)

撩动你心 2024-08-05 06:47:46

我不认为在 PHP 5.2 中可以完成“数字” :-(

在 PHP 5.3 中,如果我没记错的话,当您使用新的 (new as在 PHP >= 5.3) mysqlnd (MySQL Native Driver) 驱动程序

好吧,在深入研究我的书签后,我发现了这篇关于 mysqlnd 的文章:< a href="http://blog.ulf-wendel.de/2008/pdo_mysqlnd-the-new-features-of-pdo_mysql/" rel="noreferrer">PDO_MYSQLND:PHP 5.3 中 PDO_MYSQL 的新特性

它是这么说的(引用):

使用 mysqlnd 进行 PDO 的优点

mysqlnd 返回本机数据类型
使用服务器端准备好的语句,
例如返回 INT 列
作为整数变量而不是
细绳。 这意味着更少的数据
内部转换。

但这仅适用于 PHP 5.3(前提是您的 PHP 5.3 版本是使用 mysqlnd 编译的(而不是旧的 libmysql)),并且似乎仅适用于准备好的语句:-(

抱歉...

A解决方案是在 PHP 端建立一个映射系统(例如 ORM——参见 Doctrine ; 只是作为 ORM 的一个例子:我不知道它是否能满足您的要求) 将来自 DB 的结果转换为 PHP 数据类型......

是的,这很糟糕如果您想使用像 ===!== 这样的类型敏感运算符...

I don't think having "numbers" can be done in PHP 5.2 :-(

In PHP 5.3, it becomes possible, if I remember correctly, when you are using the new (new as in PHP >= 5.3) mysqlnd (MySQL Native Driver) driver.

Well, after more digging through my bookmarks I found this article about mysqlnd : PDO_MYSQLND: The new features of PDO_MYSQL in PHP 5.3

It says this (quote) :

Advantages of using mysqlnd for PDO

mysqlnd returns native data types when
using Server-side Prepared Statements,
for example an INT column is returned
as an integer variable not as a
string. That means fewer data
conversions internally.

But this is PHP 5.3 only (provided your version of PHP 5.3 is compiled with mysqlnd (and not old libmysql)), and seems to only be the case for prepared statements :-(

Sorry...

A solution would be to have, on the PHP-side, a mapping-system (like an ORM -- see Doctrine ; just as an example of ORM : I don't know if it does what you're asking) to convert results coming from the DB to PHP datatypes...

And yes, this is bad if you want to use operators like === and !==, which are type-sensitive...

指尖凝香 2024-08-05 06:47:46

帕斯卡的答案是正确的。 我在让它一切正常工作时遇到了一些麻烦。 这是您需要做的。

首先,确保您已经安装并激活了 mysqlnd。 查看php --info。 在 pdo_mysql 部分,它应该如下所示:

pdo_mysql

PDO Driver for MySQL => enabled
Client API version => mysqlnd 5.0.8-dev - 20102224 - $Revision: 321

如果您在客户端 API 版本中看到的不是 mysqlnd,而是类似... ...的行,

Client API version => 5.5.29

那么您还没有安装并运行 mysqlnd。 首先解决这个问题。

其次,PDO 默认情况下对所有 MySQL 连接使用模拟准备语句。 很荒谬,但确实如此。 仅当您使用真正的准备好的语句(在链接的博客文章中称为“服务器端准备好的语句”,现在是此处)。 因此,您必须将 PDO::ATTR_EMULATE_PREPARES 设置为 false,如下所示:

$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

最后,确保您没有将 PDO::ATTR_STRINGIFY_FETCHES 设置为 true。

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

Pascal's answer is correct. I had some trouble it making it all work. Here is what you need to do.

First, make sure you have mysqlnd installed and activated. Look at php --info. In the pdo_mysql section, it should look like:

pdo_mysql

PDO Driver for MySQL => enabled
Client API version => mysqlnd 5.0.8-dev - 20102224 - $Revision: 321

If instead of seeing mysqlnd in the Client API version, you see a line like...

Client API version => 5.5.29

...then you don't have mysqlnd installed and going. Take care of that first.

Second, PDO uses emulated prepared statements by default for all MySQL connections. Ridiculous, but there it is. And you will get native data types only if you use real prepared statements (called "server-side prepared statements" in the linked blog post, which is now here). So you must set PDO::ATTR_EMULATE_PREPARES to false, like so:

$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

Lastly, make sure that you have not set PDO::ATTR_STRINGIFY_FETCHES to true.

$pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
池木 2024-08-05 06:47:46

您可能已经安装了 mysqlnd,但这并不意味着它已可供常规 PDO 扩展 (pdo_mysql) 使用,这更重要共享主机上经常出现这种情况,因此请确保启用 nd_pdo_mysql 扩展,并禁用其他扩展 pdo_mysql,因为它可能会产生冲突。

@screenshot

在此处输入图像描述

You might have mysqlnd installed, but it doesn't mean it is ready for use by the regular PDO extension (pdo_mysql), this is more often the case on shared hosting, so make sure to enable the nd_pdo_mysql extension, and also to disable other extension pdo_mysql as it might create an conflict.

@screenshot

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文