I am trying to display Data in MySQL DB. But, I keep getting SyntaxError: Unexpected end of JSON input at Object.parse (native) .
My PHP file which fetch the data is
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
include_once 'config/database.php';
include_once 'objects/ideas.php';
$database = new Database();
$db = $database->getConnection();
$ideas = new ideas($db);
$stmt = $ideas->readAll();
$num = $stmt->rowCount();
if($num>0){
$data="";
$x=1;
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
// extract row
// this will make $row['name'] to
// just $name only
extract($row);
$data .= '{';
$data .= '"id":"' . $id . '",';
$data .= '"name":"' . $name . '",';
$data .= '"description":"' . html_entity_decode($description) . '",';
$data .= '"SubmitedBy":"' . $SubmitedBy . '",';
$data .= '}';
$data .= $x<$num ? ',' : ''; $x++; }
}
// json format output
echo '{"records":[' . $data . ']}';
?>
My AngularJS Code,
$scope.getAll = function() {
$http.get("read_ideas.php").success(function(response) {
$scope.names = response.records;
});
}
And the Data in MySQL DB is just
id - 1
name - me
description - desc
SubmitedBy - me
created - 2016-05-19 14:13:10
modified - 2016-05-19 17:43:10
I am not able to figure out why it throws this error again and again.
i understand it is somehow not able to parse the data in db to JSON format. But, how can i fix it.
json_encode
?!,
at the end of your last object,SubmitedBy
x) you have to remove it, so it should be$data .= '"SubmitedBy":"' . $SubmitedBy . '";