简体   繁体   English

读取json数据时获取空值

[英]While reading json data getting null value

Using my php page im reading mysql data and showing in json.使用我的 php 页面读取 mysql 数据并在 json 中显示。 Code of php page is -- php页面代码为——

<?php
    function loadData($limit){
    require "config.inc.php";

    $query = $con->prepare("SELECT * FROM UploadText ORDER BY slno DESC LIMIT $limit "); 
    $query->execute();
    $array = array(); 

    while($data = $query->fetch(PDO::FETCH_ASSOC)){
        $id = $data['slno'];
        $title = $data['textmsg']; 

        array_push($array, array(
                "id" => $id,
                "title" => $title
            )
        );
}

echo json_encode($array);
}

function loadMoreData($lastId, $limit){
    require "config.inc.php";
    try{
        $query = $con->prepare("SELECT * FROM UploadText WHERE slno < $lastId ORDER BY slno DESC LIMIT $limit "); 
        $query->execute();
        $array = array(); 

        while($data = $query->fetch(PDO::FETCH_ASSOC)){
            $id = $data['slno'];
            $title = $data['textmsg']; 

            array_push($array, array(
                    "id" => $id,
                    "title" => $title
                )
            );
        }

        echo json_encode($array);
    } catch(Exception $e){
        die($e->getMessage());
    }
}



if(isset($_GET['action']) && $_GET['action'] == "apiText"){
    $lastId = $_GET['lastId'];
    // this is teh limit set in the android java code (LOAD_LIMIT)
    $limit = $_GET['limit'];
    loadMoreData($lastId, $limit);
} else {
    $limit = $_GET['limit'];
    loaddata($limit);
}
?>

And this is the output of the above code --这是上面代码的输出——

    [{"id":"14","title":"A Kid On His Way 2 Home With His Mom\r\nSaw A Couple Kissing On The Road,\r\nHe Suddenly Shouted & Said:\r\nLook Mom look, that boy and girl\r\nAre Fighting For A Chewing GUM."},
{"id":"13","title":null},
{"id":"12","title":null},
{"id":"11","title":null},
{"id":"10","title":"PAPPU : Daddy, have you\never been to Egypt?\nFATHER : No. Why do\nyou ask that?\nPAPPU: Well, where did\nyou get THIS mummy??"},
{"id":"9","title":"Y r u so opposite to me?\nWhen i say tea,u say coffee!\nI say white,u say black!\nI went to dental hospital,u went to mental hospital!\nI came back and u still there!"}]

Not able to understand, why its reading null values.无法理解,为什么它读取空值。 Data is present in mysql table, but then also reading some null values and some data are shown properly.数据存在于 mysql 表中,但随后也读取了一些空值,并且一些数据显示正确。

This is the structure of my mysql table --这是我的mysql表的结构—— 在此处输入图片说明

Finally resolved the issue by googling, the issue is encoding problem --最后通过谷歌搜索解决了这个问题,问题是编码问题——

$title = utf8_encode($data['textmsg']); 

This resolved the problem.这解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM