在读取json数据时获取null值

时间:2017-12-27 13:23:45

标签: php mysql json

使用我的php页面我正在读取mysql数据并在json中显示。 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);
}
?>

这是上述代码的输出 -

    [{"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!"}]

无法理解,为什么它读取空值。 数据存在于mysql表中,但随后也读取了一些空值,并且正确显示了一些数据。

这是我的mysql表的结构 - enter image description here

1 个答案:

答案 0 :(得分:0)

最后通过谷歌搜索解决了问题,问题是编码问题 -

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

这解决了这个问题。