首页 文章

PHP Json编码PDO :: FETCH_ASSOC

提问于
浏览
0

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

When I use this and echo the $json I get a blank page. When I do a var_dump on that var I get a bool, false.

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

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

However, if I was to place the same fetchAll into a result var and print it, it works fine!

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

我已经读过其他有类似问题的人,并且这是一个UTF8编码问题,所以我试图在json_encode之前在$数组上执行utf8_encode,但结果与空白页相同 . 有谁能解释一下?

1 回答

  • 1

    json_encode 是字符编码敏感 . 如果无法处理编码,它将失败 . print_r 不是 . 它会愉快地打印出你给它的任何东西 .

    只有在源数据中的字符串编码为ISO-8859-1时, utf8_encode 修复才有效 . 假设这是真的它应该工作 . 确保你这样做...... https://stackoverflow.com/a/2790107/111755

相关问题