简体   繁体   中英

While reading json data getting null value

Using my php page im reading mysql data and showing in json. Code of php page is --

<?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.

This is the structure of my mysql table -- 在此处输入图片说明

Finally resolved the issue by googling, the issue is encoding problem --

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

This resolved the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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