1

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.

6
  • 3
    What does the resulting JSON look like? Have you tested it is valid? Why on earth are you generating it by bashing strings together instead of using json_encode?! Commented May 19, 2016 at 12:34
  • jsonlint.com is good online tool to check your JSON structure. Commented May 19, 2016 at 12:38
  • Tried the 'echo json_encode($row);' still its not working. Commented May 19, 2016 at 12:41
  • 1
    You actually have an extra , at the end of your last object, SubmitedBy x) you have to remove it, so it should be $data .= '"SubmitedBy":"' . $SubmitedBy . '"; Commented May 19, 2016 at 12:45
  • @MiguelGuerreiro, thanks it worked. Commented May 20, 2016 at 6:26

2 Answers 2

1

You are actually adding a , at the last value of SubmitedBy replace this

$data .= '"SubmitedBy":"' . $SubmitedBy  . '",';

with this

$data .= '"SubmitedBy":"' . $SubmitedBy;
Sign up to request clarification or add additional context in comments.

1 Comment

I tried, still its giving me below error angular.min.js:117 SyntaxError: Unexpected end of JSON input at Object.parse (native)
0

You actually have an extra , at the end of your last object, SubmitedBy x) you have to remove it, so it should be

 $data .= '"SubmitedBy":"' . $SubmitedBy . '";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.