虽然阅读JSON数据获取空值

使用我的PHP页面我读mysql数据并显示在JSON中。 PHP页面的代码 -虽然阅读JSON数据获取空值

<?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表的结构 -

回答:

通过谷歌搜索终于解决了这个问题,这个问题是编码问题 -

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

这解决了这个问题。

以上是 虽然阅读JSON数据获取空值 的全部内容, 来源链接: utcz.com/qa/265318.html

回到顶部