0

I have searched for that error and looked up many posts.. but i still can't figure out what is wrong with this code here:

my ajax call:

function myCall3() {
  $.ajax({
    type:"POST",
    url:"ajax3.php",
    dataType:"json",
    success:function(response){
      alert(response[0]);
    }
  });
}

my mysql/php code:

<?php


// QUERY NEW VINE
$array = array();
$myquery = "SELECT * FROM table1 ORDER BY rand() LIMIT 9";
$result = mysql_query($myquery)
OR die("Error: $myquery <br>".mysql_error());

while($row = mysql_fetch_object($result)){
  $currentid = "$row->id";
  $currentname = "$row->name";
  $currenturl = "$row->url";
  $currentimage = "$row->image";
  $array[] =     array('id'=>$currentid,'url'=>$currenturl,'name'=>$currentname,'image'=>$currentimage);

}

echo json_encode($array);

?>

when i alert the error it says:

SyntaxError: SyntaxError: JSON.parse: unexpected character

3 Answers 3

2

You keep overwriting $array in your loop, and then echoing it out too soon.

$array =    array('id'=>$currentid,'url'=>$currenturl,'name'=>$currentname,'image'=>$currentimage);

echo json_encode($array);

}

you should push each row to the $array variable, and then json_encode outside the loop.

$array = array();
$myquery = "SELECT * FROM table1 ORDER BY rand() LIMIT 9";
$result = mysql_query($myquery)
OR die("Error: $myquery <br>".mysql_error());
while($row = mysql_fetch_object($result))
{
$currentid = "$row->id";
$currentname = "$row->name";
$currenturl = "$row->url";
$currentimage = "$row->image";
$array[] =  array('id'=>$currentid,'url'=>$currenturl,'name'=>$currentname,'image'=>$currentimage);

}

echo json_encode($array);

then your javascript callback will need to expect an array, not an object, i.e.

console.log(response[0]);

note: you could just push $row directly onto $array of you wanted to.

Sign up to request clarification or add additional context in comments.

12 Comments

When i use this code, how would i alert all the urls in js for example? I am just producing errors -.-
what error? what do you get when you log response and response[0]? It should just all be straightforward array / object notation at this point.
When i log response[0] it says: SyntaxError: JSON.parse: unexpected character
what about response then? start at the beginning if you have to. this is basic debugging 101. what do you see in your developer tools? Is it valid JSON? (use jsonlint.com)
Thank you! I am still learning.. so much stuff that i don't know. But you helped me a lot. Finally figured it out.Thanks!
|
1
$newArray = array();
while($row = mysql_fetch_object($result) {
   $newArray[] = $row;
}
echo json_encode($newArray);

Then, in JS you have to iterate through the array.

Comments

0

Your output looks like this:

{"id":123,"url":"http://.../","name":"Blah","image":"blah.png"}{"id":456,.....

That doesn't look right to me :p

It looks like your JavaScript is expecting just one row, so try LIMIT 1.

2 Comments

yes it works with limit1, but the thing is i want to get 9 :).. do i may have to change the variables to arrays in the ajax call?
@marc collect the results into an array and stringify and echo it at once.

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.