PHP Json编码PDO :: FETCH_ASSOC [英] PHP Json Encode PDO::FETCH_ASSOC

查看:118
本文介绍了PHP Json编码PDO :: FETCH_ASSOC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图返回我的MSSQL数据库表的所有行,并将它们吐入JSON_ENCODE中.

I'm attempting to return all the rows of my MSSQL database table and spit them out in a JSON_ENCODE.

当我使用它并回显$ json时,我得到一个空白页.当我在该var上执行var_dump时,我得到一个布尔值,为false.

$sth = $db->prepare("SELECT * FROM dbo.Devices");
$sth->execute();

$array = $sth->fetchAll( PDO::FETCH_ASSOC );
$json = json_encode($array);

但是,如果我将相同的fetchAll放入结果var中并打印,效果很好!

Working using print function.
$result = $sth->FetchAll();
print_r($result);

我已经读过其他类似的问题,这是一个UTF8编码问题,因此我尝试在$ array上执行utf8_encode,然后再执行json_encode,但结果与空白页相同.谁能解释一下?

I've read of others having similar issues and that it was a UTF8 encoding issue so I attempted to do a utf8_encode on the $array before a json_encode but with the same result of a blank page. Can anyone explain this?

推荐答案

json_encode对字符编码敏感.如果无法处理编码,它将失败. print_r不是.它会愉快地打印出您提供的任何内容.

json_encode is character encoding sensitive. It will fail if it can't handle the encoding. print_r is not. It will happily print out whatever you give it.

仅当源数据中的字符串编码为ISO-8859-1时,utf8_encode修复程序才有效.假设这是正确的,那么它应该可以工作.确保您这样做... https://stackoverflow.com/a/2790107/111755

The utf8_encode fix will only work if the strings in your source data are encoded as ISO-8859-1. Assuming that's true it should work. Make sure you do it like this... https://stackoverflow.com/a/2790107/111755

这篇关于PHP Json编码PDO :: FETCH_ASSOC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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