PHP + PDO + MySQL:如何从MySQL返回整数和数字列作为PHP中的整数和数字? [英] PHP + PDO + MySQL: how do I return integer and numeric columns from MySQL as integers and numerics in PHP?

查看:68
本文介绍了PHP + PDO + MySQL:如何从MySQL返回整数和数字列作为PHP中的整数和数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Stack Overflow上看到了这个问题重复了几次,但是没有一个人充分探讨这个问题(或者至少以一种对我有帮助的方式)

问题在于,数据库查询应在PHP中为整数列返回整数数据类型.而是查询将每一列作为字符串类型返回.

The problem is that a DB query should return integer data types in PHP for integer columns. Instead the query returns every column as a string type.

我确保如果 false 为"PDO :: ATTR_STRINGIFY_FETCHES",只是为了确保结果不会被强制转换为字符串.

I've ensured that "PDO::ATTR_STRINGIFY_FETCHES" if false just to make sure results aren't being cast to string.

我见过的答案:

  • 无法完成
    • 不是,它可以在安装了PHP/MySQL的Mac OS X上运行
    • It can't be done
      • Nope, it's working on Mac OS X installed PHP/MySQL
      • 不,我不会那样做
      • 我的数据以JSON输出并被许多其他服务使用,其中一些服务要求数据采用正确的格式

      从我的研究中,我知道这是一个驱动程序实现问题.

      From my research I understand that this is a driver implementation issue.

      许多消息来源声称MySQL本机驱动程序不支持返回数字类型.这似乎并不正确,因为它可以在Mac OS X上运行.除非他们的意思是说" Linux 上的MySQL本机驱动程序不支持该功能".

      Many sources claim that the MySQL native driver does not support returning numeric types. This doesn't seem true since it works on Mac OS X. Unless they mean to say that "the MySQL native driver on Linux doesn't support the feature".

      这意味着我在Mac OS X上安装的驱动程序/环境有一些特殊之处.我一直在尝试找出差异以便应用修复程序,但是我对如何检查的知识有所限制.这些东西.

      This implies that there is something special about the driver/environment I have installed on Mac OS X. I've been trying to identify the differences in order to apply a fix but I'm limited by my knowledge of how to check these things.

      • OS X上的PHP是通过Home Brew编译并安装的.
      • Ubuntu上的PHP是通过"apt-get install php5-dev"安装的.
      • OS X上的PHP正在连接到也在OS X上运行的MySQL服务器
        • 服务器版本:5.1.71-log源分发
        • PHP on OS X was compiled and installed via Home Brew
        • PHP on Ubuntu was installed via "apt-get install php5-dev"
        • PHP on OS X is connecting to a MySQL server also running on OS X
          • Server version: 5.1.71-log Source distribution
          • 服务器版本:5.1.66-0 + squeeze1(Debian)
          • 版本:10.04.1
          • PHP 5.4.21-1 + debphp.org〜lucid + 1(cli)(内置:2013年10月21日08:14:37)
          • php -i

          • Version: 10.04.1
          • PHP 5.4.21-1+debphp.org~lucid+1 (cli) (built: Oct 21 2013 08:14:37)
          • php -i

          pdo_mysql

          pdo_mysql

          用于MySQL的PDO驱动程序=>已启用 客户端API版本=> 5.1.72

          PDO Driver for MySQL => enabled Client API version => 5.1.72

          • 10.7.5
          • PHP 5.4.16(cli)(内置:2013年8月22日09:05:58)
          • php -i

          • 10.7.5
          • PHP 5.4.16 (cli) (built: Aug 22 2013 09:05:58)
          • php -i

          pdo_mysql

          pdo_mysql

          用于MySQL的PDO驱动程序=>已启用 客户端API版本=> mysqlnd 5.0.10-20111026-$ Id:e707c415db32080b3752b232487a435ee0372157 $

          PDO Driver for MySQL => enabled Client API version => mysqlnd 5.0.10 - 20111026 - $Id: e707c415db32080b3752b232487a435ee0372157 $

          PDO::ATTR_CASE => PDO::CASE_NATURAL,
          PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
          PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
          PDO::ATTR_STRINGIFY_FETCHES => false,
          PDO::ATTR_EMULATE_PREPARES => false,
          

          任何帮助和专业知识将不胜感激:)如果找到答案,我一定会在这里发回邮件.

          Any help and expertise would be appreciated :) I'll definitely be posting back here if I find the answer.

          推荐答案

          解决方案是确保您使用php的 mysqlnd 驱动程序.

          The solution is to ensure that you are using the mysqlnd driver for php.

          查看php -i时,将没有提及"mysqlnd". pdo_mysql部分将具有以下内容:

          When viewing php -i, there will be no mention of "mysqlnd". The pdo_mysql section will have something like this:

          pdo_mysql
          
          PDO Driver for MySQL => enabled Client API version => 5.1.72
          

          如何安装?

          大多数有关L/A/M/P的安装指南都建议使用apt-get install php5-mysql,但是MySQL的本机驱动程序是通过其他软件包安装的:php5-mysqlnd.我发现 ppa:ondrej/php5-oldstable 可用.

          How do you install it?

          Most installation guides for L/A/M/P suggest apt-get install php5-mysql but the native driver for MySQL is installed by a different package: php5-mysqlnd. I found that this was available with the ppa:ondrej/php5-oldstable.

          要切换到新驱动程序(在Ubuntu上):

          To switch to the new driver (on Ubuntu):

          • 删除旧驱动程序:
            apt-get remove php5-mysql
          • 安装新驱动程序:
            apt-get install php5-mysqlnd
          • 重新启动apache2:
            service apache2 restart
          • Remove the old driver:
            apt-get remove php5-mysql
          • Install the new driver:
            apt-get install php5-mysqlnd
          • Restart apache2:
            service apache2 restart

          现在php -i将在pdo_mysql部分中明确提及"mysqlnd":

          Now php -i will mention "mysqlnd" explicitly in the pdo_mysql section:

          pdo_mysql
          
          PDO Driver for MySQL => enabled
          Client API version => mysqlnd 5.0.10 - 20111026 - $Id:      e707c415db32080b3752b232487a435ee0372157 $
          

          PDO设置

          确保PDO::ATTR_EMULATE_PREPARESfalse(检查默认设置或进行设置):
          $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

          PDO settings

          Ensure that PDO::ATTR_EMULATE_PREPARES is false (check your defaults or set it):
          $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

          确保PDO::ATTR_STRINGIFY_FETCHESfalse(检查默认设置或进行设置):
          $pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);

          Ensure that PDO::ATTR_STRINGIFY_FETCHES is false (check your defaults or set it):
          $pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);

          • 浮点类型(FLOAT,DOUBLE)作为PHP浮点数返回.
          • 整数类型(INTEGER,INT,SMALLINT,TINYINT,MEDIUMINT,BIGINT†)作为PHP整数返回.
          • 定点类型(DECIMAL,NUMERIC)作为字符串返回.

          †值大于64位带符号的int(9223372036854775807)的BIGINT将作为字符串返回(或在32位系统上为32位)

          † BIGINTs with a value greater than a 64 bit signed int (9223372036854775807) will return as a string (or 32 bits on a 32 bit system)

              object(stdClass)[915]
                public 'integer_col' => int 1
                public 'double_col' => float 1.55
                public 'float_col' => float 1.5
                public 'decimal_col' => string '1.20' (length=4)
                public 'bigint_col' => string '18446744073709551615' (length=20)
          

          这篇关于PHP + PDO + MySQL:如何从MySQL返回整数和数字列作为PHP中的整数和数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆