0

I have a loop that grabs DB values and creates an object. I want to echo a JSON object every time the loop is iterated through. As of now, I just get blank in my console. Also, those commented out printf statements work fine. Any help would be much appreciated!

AJAX call

$.ajax({
        url:'ajax/ipGet.php',
        type: 'GET',
        dataType:'json',
        success:function(response){
            console.log(response);
        }

    });

PHP Data Generation

$infoArray = new Array();
while($row = mysqli_fetch_array($getIpQuery, MYSQLI_NUM)){
    for($x=0;$x<count($row);$x++)
    {
        $getIpInfo = mysqli_query($dbcon,"SELECT * FROM ipInfo WHERE address='$row[$x]'");
        $retrievedInfo = mysqli_fetch_array($getIpInfo,MYSQLI_NUM);
        $ipInfo->ipAddress = $retrievedInfo[0];
        $ipInfo->port = $retrievedInfo[1];
        $ipInfo->status = getStatus($ipInfo->ipAddress, $ipInfo->port);
        array_push($infoArray,$ipInfo);

    } 
  }

echo json_encode($infoArray);

Thanks for any help!

~Carpetfizz

2
  • 1
    I don't see why this question is downvoted. The question is clear to understand, according code is added and everything is well formatted. It would be great if downvoters could comment their decisions. I give you +1 to compensate. Commented Oct 20, 2013 at 20:48
  • Thanks! Yeah, like you said it would be helpful if they said what I did wrong. Commented Oct 20, 2013 at 20:50

1 Answer 1

2

json must be a single self-contained entity. You're outputting MULTIPLE separate bits of JSON text, which is incorrect.

JSON encoding should be the very LAST thing you do, e.g.

while(...) {
   $array[] = ....
}
echo json_encode($array);

Consider what you're doing from the client perspective... building an array of ipinfo, so you run the loop once and produce

{"ipAddress":"127.0.0.1","port":80,....}

But you're doing it multple times, so you end up with

{"ipAddress":"127.0.0.1","port":80,....}{"ipAddress":"127.0.0.1","port":80,....{"ipAddress":"127.0.0.1","port":80,....}

as your output, and now you've got illegal javascript. Remember that JSON text is essentialy the right-hand-side of a Javascript assignment operation, e.g.

var somevar = ...json_goes_here...;

So you're doing

var stuff = {...}{...}{...};

which is an outright syntax error, instead of

var stuff = [{...},{...},{...}];

which would be correct.

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

6 Comments

Thanks! So, when I get the array in Javascript, I can access each object with var object = stuff[x] ?
yes. json's just a transport/encoding format. you should NEVER have to deal with the raw json string itself. you decode the string into a native structure, deal with that structure, then re-encoding into json for shipment.
Cool, so I updated to the code in the original post-but I'm still getting the same issue.
your db stuff is also another problem. you're running nested queries, which sould be rewritten as a single joined query. and you seem to be treating mysql_fetch calls as returning the ENTIRE result set. they only return a single row of the result.
Right, so what happens is that each user has a list of IPs. There's another table that stores information for the IPs. So, for all the IPs under the user, I want to be able to build a JSON object, then send it out to the client side. The second query is to get all the information for the IP in question.
|

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.