php 如何使用 PDO 从 MySQL 获取数字类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1197005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get numeric types from MySQL using PDO?
提问by victrnava
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?
我正在使用 PDO 和 MySQL,出于某种原因,当从数据库中获取 int 类型的值时,PDOStatement 返回数字的字符串表示形式,而不是数字类型的值。我如何防止这种情况发生?
I noticed there is a attribute of the PDO class: PDO::ATTR_STRINGIFY_FETCHESthat 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.
我注意到 PDO 类有一个属性:PDO::ATTR_STRINGIFY_FETCHES它应该解决这个问题,但是在尝试修改它时,它抛出一个错误,指出该属性对 MySQL 驱动程序无效。
Is it normal to get strings instead of numbers when consulting a database?
查询数据库时获取字符串而不是数字是否正常?
采纳答案by Pascal MARTIN
I don't think having "numbers" can be done in PHP 5.2 :-(
我不认为在 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.
在 PHP 5.3 中,如果我没记错的话,当您使用新的(PHP >= 5.3 中的新)mysql nd (MySQL 本机驱动程序)驱动程序时,它成为可能。
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
好吧,在对我的书签进行更多挖掘之后,我发现了这篇关于 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.
将 mysqlnd 用于 PDO 的优点
mysqlnd 在使用服务器端准备语句时返回本机数据类型,例如 INT 列作为整数变量而不是字符串返回。这意味着更少的内部数据转换。
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 :-(
但这仅适用于 PHP 5.3(前提是您的 PHP 5.3 版本是使用 mysqlnd(而不是旧的 libmysql)编译的),并且似乎仅适用于准备好的语句:-(
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...
一个解决方案是在 PHP 端使用映射系统(如 ORM - 请参阅Doctrine;仅作为 ORM 的一个示例:我不知道它是否满足您的要求)来转换结果从 DB 到 PHP 数据类型...
And yes, this is bad if you want to use operators like ===and !==, which are type-sensitive...
是的,如果你想使用像===and!==这样的运算符,这很糟糕,它们是类型敏感的......
回答by Josh Davis
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.
首先回答你的最后一个问题,“是的”,不幸的是,将数字作为字符串接收是正常的。正如 Pascal 引用的手册所说,mysqlnd(PHP 5.3)将从准备好的语句返回本机数据类型,前提是您从 PDO 关闭准备好的语句模拟。
new PDO($dsn, $user, $pass, array(
PDO::ATTR_EMULATE_PREPARES => false
))
PDO::ATTR_STRINGIFY_FETCHES is unrelated to MySQL.
PDO::ATTR_STRINGIFY_FETCHES 与 MySQL 无关。
If you look at the bright side, it's good practice to use prepared statements anyway, so... ;)
如果你从好的方面看,无论如何使用准备好的语句是一种很好的做法,所以...... ;)
回答by Alexander Garden
Pascal's answeris 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:
首先,确保你已经安装并激活了 mysqlnd。看看php --info。在 pdo_mysql 部分,它应该如下所示:
pdo_mysql
PDO Driver for MySQL => enabled
Client API version => mysqlnd 5.0.8-dev - 20102224 - $Revision: 321
If instead of seeing mysqlndin the Client API version, you see a line like...
如果不是mysqlnd在客户端 API 版本中看到,而是看到一行类似...
Client API version => 5.5.29
...then you don't have mysqlnd installed and going. Take care of that first.
...那么你没有安装和运行mysqlnd。先管好。
Second, PDO uses emulated prepared statements by default for all MySQLconnections. 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默认为所有 MySQL连接使用模拟准备好的语句。可笑,但就是这样。并且只有当您使用真正的准备好的语句(在链接的博客文章中称为“服务器端准备好的语句”,现在在这里)时,您才会获得本机数据类型。因此,您必须将 PDO::ATTR_EMULATE_PREPARES 设置为 false,如下所示:
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
Lastly, make sure that you have not set PDO::ATTR_STRINGIFY_FETCHES to true.
最后,确保您没有将 PDO::ATTR_STRINGIFY_FETCHES 设置为 true。
$pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
回答by AamirR
You might have mysqlndinstalled, but it doesn't mean it is ready for use by the regular PDOextension (pdo_mysql), this is more often the case on shared hosting, so make sure to enable the nd_pdo_mysqlextension, and also to disable other extension pdo_mysqlas it might create an conflict.
您可能已经mysqlnd安装,但这并不意味着它可以供常规PDO扩展使用(pdo_mysql),在共享主机上更常见这种情况,因此请确保启用nd_pdo_mysql扩展,并禁用其他扩展,pdo_mysql因为它可能会创建一个冲突。
@screenshot
@截屏
I found this (duplicate) question, decided to post my thoughts here, hope its fine ;)
我发现了这个(重复的)问题,决定在这里发表我的想法,希望没问题;)


