如何使用 PDO 从 MySQL 获取数字类型?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先回答你的最后一个问题,“是的”,不幸的是,接收数字作为字符串是正常的。 正如 Pascal 引用的手册所说,mysqlnd(PHP 5.3)将从准备好的语句返回本机数据类型,前提是您关闭 PDO 的准备好的语句模拟。
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.
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... ;)
我不认为在 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 的新特性
它是这么说的(引用):
但这仅适用于 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) :
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...帕斯卡的答案是正确的。 我在让它一切正常工作时遇到了一些麻烦。 这是您需要做的。
首先,确保您已经安装并激活了 mysqlnd。 查看
php --info
。 在 pdo_mysql 部分,它应该如下所示:如果您在客户端 API 版本中看到的不是
mysqlnd
,而是类似... ...的行,那么您还没有安装并运行 mysqlnd。 首先解决这个问题。
其次,PDO 默认情况下对所有 MySQL 连接使用模拟准备语句。 很荒谬,但确实如此。 仅当您使用真正的准备好的语句(在链接的博客文章中称为“服务器端准备好的语句”,现在是此处)。 因此,您必须将 PDO::ATTR_EMULATE_PREPARES 设置为 false,如下所示:
最后,确保您没有将 PDO::ATTR_STRINGIFY_FETCHES 设置为 true。
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:If instead of seeing
mysqlnd
in the Client API version, you see a line like......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:
Lastly, make sure that you have not set PDO::ATTR_STRINGIFY_FETCHES to true.
您可能已经安装了
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 regularPDO
extension(pdo_mysql)
, this is more often the case on shared hosting, so make sure to enable thend_pdo_mysql
extension, and also to disable other extensionpdo_mysql
as it might create an conflict.@screenshot