php pdo fetch num,php – PDO Fetch数据返回字符串数组

我试图从PDO的MySQL数据库中获取数据但不幸的是PDO将结果作为字符串数组返回.我想将原生MySQL数据类型保留在结果数组中.

我已经尝试将PDO :: ATTR_DEFAULT_FETCH_MODE设置为PDO :: FETCH_ASSOC和PDO :: FETCH_OBJ,但它仍然将INT数据作为字符串返回.

这是转储的结果:

array (size=1)

0 =>

object(stdClass)[27]

public 'id' => string '3' (length=1)

public 'avatar' => string '' (length=0)

public 'fullName' => string 'Mikheil Janiashvili' (length=19)

public 'email' => string 'xxxxx@yyyyy.com' (length=17)

public 'phone' => string '23 3537 20 03544' (length=12)

public 'educationGE' => string '' (length=0)

public 'educationEN' => string '' (length=0)

public 'educationRU' => string '' (length=0)

public 'experienceGE' => string '' (length=0)

public 'experienceEN' => string '' (length=0)

public 'experienceRU' => string '' (length=0)

public 'descriptionGE' => string '' (length=0)

public 'descriptionEN' => string '' (length=0)

public 'descriptionRU' => string '' (length=0)

解决方法:

当您实例化PDO对象时,您需要告诉它使用MySQL的本机准备查询:

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

假设你正在使用PHP> = 5.3,你将使用mysqlnd库,它可以从准备好的查询中返回正确的数据类型.

例:

$php -a

Interactive shell

php > $db = PDO("mysql:host=localhost;dbname=test", "test", "");

php > $res = $db->query("SELECT 1, PI()");

php > var_dump($res->fetch());

array(4) {

[1]=>

string(1) "1"

[2]=>

string(1) "1"

["PI()"]=>

string(8) "3.141593"

[3]=>

string(8) "3.141593"

}

php > $db = PDO("mysql:host=localhost;dbname=test", "test", "", [PDO::ATTR_EMULATE_PREPARES=>false]);

php > $res = $db->query("SELECT 1, PI()");

php > var_dump($res->fetch());

array(4) {

[1]=>

int(1)

[2]=>

int(1)

["PI()"]=>

float(3.1415926535898)

[3]=>

float(3.1415926535898)

}

php >

标签:php,mysql,pdo

来源: https://codeday.me/bug/20190702/1356489.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值