为什么回显JSON编码的数组不会产生任何输出 [英] Why echoing JSON encoded arrays won't produce any output

查看:132
本文介绍了为什么回显JSON编码的数组不会产生任何输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有19个元素的小数据库,这是我检索数据库的PHP函数:

I've a little DB with 19elements, that's my PHP function to retrieve the DB:

function retrieveDB(){

    $tempDB=array();

    $database=new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
    if(mysqli_connect_errno()){ 
        echo "Errore in connessione al database: ".mysqli_connect_error();
        exit();
    }
    $query="SELECT * FROM dr ORDER BY indice;";
    $risultato=$database->query($query);

    while($row = $risultato->fetch_assoc()){

        $tempDB[]=$row;
    }

    $database->close();
    return $tempDB;
}

我都尝试过

$temp=retrieveDB();
echo json_encode($temp);

foreach($temp as $t){
echo json_encode($t);}

,但是似乎只在19个元素上打印3个元素,而跳过其他元素.那是我的输出:

but it seems to print only 3 elements on 19, skipping the others. That's my output:

{"indice":"1","domanda":"Quale principio attivo fa passare quel brutto mal di vivere?","risposta":"PARACETAMOLO","visibile":"1"}


{"indice":"4","domanda":"SARTANO utilizzato nella profilassi dell'ictus?","risposta":"LOSARTAN","visibile":"1"}













{"indice":"18","domanda":"Quale farmaco aumenta la concentrazione ematica della digossina? ","risposta":"VERAPAMIL","visibile":"1"}

我还在utf8中编码了值.有什么建议吗?

I also encoded values in utf8. Any advices?

推荐答案

从数据库中检索到的字符串未编码为UTF-8(我想它们是ISO-8859-1).这就是json_encode()失败的原因.它会触发警告json_encode(): Invalid UTF-8 sequence in argument,但您的设置可能不允许PHP显示错误.

The strings you retrieve from the database are not encoded as UTF-8 (they are ISO-8859-1, I presume). This is why json_encode() fails. It triggers a warning that says json_encode(): Invalid UTF-8 sequence in argument but your setup probably does not allow PHP display the errors.

error_reporting(E_ALL & ~E_NOTICE); ini_set('display_errors', '1');放在脚本顶部的某个位置,您会看到它们.

Put error_reporting(E_ALL & ~E_NOTICE); ini_set('display_errors', '1'); somewhere on top of your script and you'll see them.

这篇关于为什么回显JSON编码的数组不会产生任何输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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