【问题标题】:Why some string/text can not be caught with PHP json_decode?为什么 PHP json_decode 无法捕获某些字符串/文本?
【发布时间】:2014-08-17 04:58:15
【问题描述】:

我尝试从数据库中获取名称和地址以及其他数据,其中名称和地址属性为文本格式,而其他属性为 varchar 格式。我不明白为什么其中一些名称和地址在数据库中时无法被捕获。从大约 5000 条记录中,有大约 300 条记录无法在我的程序中捕获名称和地址。我研究后得到的是几个以空格开头的名称和地址。所以在我删除那些空白之后,它工作得很好。但对于其他人,我就是不明白。它们看起来只是普通的文本。代码如下:

$hasil_query1= json_encode($data->getData($query));
$obj1 = json_decode($hasil_query1,true);
$name=$obj1[0]['name'];
$address=$obj1[0]['address'];

当我打印 $name 或 $adress 时,其中一些不会出现(未捕获)。但是当我使用这段代码而不是上面的代码时,它工作得很好

$result = mssql_query($query);

while($row = mssql_fetch_array($result))
{
$name=$row['name'];
$address=$row['address'];
}

请帮助我理解这一点,因为我直到现在才看到它。

感谢您的提前。

【问题讨论】:

标签: php sql


【解决方案1】:

试试这个

$hasil_query_baru = array();

$hasil_query1= $data->getData($query);

foreach($hasil_query1 as $row){
    $hasil_query_baru[] = array_value($row);
}

$hasil_query1 = json_encode($hasil_query_baru);


...

【讨论】:

    【解决方案2】:

    var_dump 可能有助于理解问题。

    var_dump($data->getData($query));
    

    【讨论】:

      【解决方案3】:

      我从php json_encode() show's null instead of text找到了答案

      "json_encode 要求数据中的字符串被编码为 UTF-8。

      如果还没有,请将它们转换为 UTF-8"

      ~菲哈格

      感谢死亡外科医生给了我这个链接。

      【讨论】:

        猜你喜欢
        • 2013-11-14
        • 1970-01-01
        • 2013-11-08
        • 1970-01-01
        • 2014-01-02
        • 1970-01-01
        • 1970-01-01
        • 2022-07-06
        • 1970-01-01
        相关资源
        最近更新 更多