1

Hello I am trying to parse a data result from my database into my html view. I am not quite sure what is wrong with what I'm doing.

Here is my code:

function display($dbhandler){

$sql = "SELECT * FROM users";
    foreach($dbhandler->query($sql) as $row){
        $data = array('id'=>$row['id'], 
                         'email'=> $row['email'], 
                         'name'=> $row['name']
                         );

        echo json_encode($data);        

    }

}

This will give me an output of:

{"id":"1","email":"Email 1","name":"Name 1"}{"id":"2","email":"Email 2","name":"Name 2"}'

Now when I return and try to parse it using $.parseJSON(result) in jQuery.

function display_list(){

		action='select';
		
		$.ajax({
			type:"POST",
			url:"options.php",
			data:{ action : action },
			success: function(res){
				console.log(res)
				results = $.parseJSON(res);
				console.log(results);
			}
		});
	}

I get this error:

Uncaught SyntaxError: Unexpected token { in JSON at position 66 at Function.parse [as parseJSON] () at Object.success (actions.js:16) at i (jquery-3.1.1.min.js:2) at Object.fireWith [as resolveWith] (jquery-3.1.1.min.js:2) at A (jquery-3.1.1.min.js:4) at XMLHttpRequest. (jquery-3.1.1.min.js:4)

Is there anything wrong with the data I am trying to pass? Any suggestions are welcome thanks.

1
  • the json returned is not valid, try posting that json on this website jsonlint.com and see that the json you produce is not valid Commented Apr 20, 2017 at 5:34

4 Answers 4

1

Change following lines:

echo json_encode()($data);

to

echo json_encode($data);

and remove this line from the loop and put just after the loop ends. And make following changes:

success: function(res){
  var data = JSON.parse(res);
  alert(data[0].name); 
}
Sign up to request clarification or add additional context in comments.

1 Comment

I don't quite understand can you provide snippet so I can based on what was change?
0

try this

function display($dbhandler){

$sql = "SELECT * FROM users";
    foreach($dbhandler->query($sql) as $row){
        $data[] = array('id'=>$row['id'], 
                         'email'=> $row['email'], 
                         'name'=> $row['name']
                         );



    }
    echo json_encode($data);        
}

i just added the [] braces after your $data which is equivalent to array_push see php arr_push()

3 Comments

still the same error i think the data is already an array when i use print_r it show that it is
what is the output json using the above changes? because what you have on top produces an invalid json in the first place, see my comment on your question
you are right there is something wrong with the way the data is being pass from the server side i figured what was wrong and it works now
0
function display($dbhandler){
$sql = "SELECT * FROM users";
$data = array();
    foreach($dbhandler->query($sql) as $row){
        $data[] = array('id'=>$row['id'], 
                         'email'=> $row['email'], 
                         'name'=> $row['name']
                         );
    }
echo json_encode($data);       
        }

On jQuery function:

function display_list(){

        action='select';

        $.ajax({
            type:"POST",
            url:"options.php",
            dataType: 'json',
            data:{ action : action },
            success: function(json){
                console.log(json)

            }
        });
    }

4 Comments

The error is gone but it's not displaying any output
have you tried checking the response of Ajax call and checking the console bar as well. share the results please.
Yes I did it was completely blank when i tried it the other one was the error I said before although I did notice something different when I tried to push the array the first entry was duplicated which I find weird
use print_r to get the $row record, if it comes try putting print_r before json_encode and inspect the data.
0

Turns out the problem was within the server side of the script so I amended my code based on your suggestions and this is what I arrived with that works for me.

function display($dbhandler){

    $sql = "SELECT * FROM users";
    $result = array();
    foreach($dbhandler->query($sql) as $row){
        $data = array('id'=>$row['id'], 
                         'email'=> $row['email'], 
                         'name'=> $row['name']
                         );

        array_push($result,$data);      

    }

    echo json_encode($result);

}

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.