Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm having problems decoding a JSON object. I call a PHP script using an Ajax call. The PHP script returns a JSON encoded object which I can read but only the first record. How can I decode the JSON object? I am using JQUERY Mobile.

PHP script:

<?php

    $host = "localhost";
    $user = "root";
    $pass = "[password]";

    $databaseName = "zombieSurvival";
    $tableName = "TBLusers";

    //Connect to mysql database

    include 'DB.php';
    $con = mysql_connect($host,$user,$pass);
    $dbs = mysql_select_db($databaseName, $con);

    // 2) Query database

    $result = mysql_query("SELECT * FROM $tableName");
    $array = mysql_fetch_array($result);
    echo json_encode($array);

?>

and here is the Ajax call:

$.ajax({
       url: '/PHP/getUserMarkers.php',
       data: "",
       dataType: 'json',
       success: function(data){

       //How can I treat 'data' variable to make it a
       //javascript array?


       }
       });
share|improve this question
3  
Even if this is a localhost machine, posting your password is not advisable. –  JakeGould Jun 17 at 20:54
    
You can see an answer in this duplicate: Converting JSON Object into Javascript array –  Iulian Miu Jun 17 at 20:56
    
Thanks Jake - oversight on my part... Gonna take a looking at the duplicate posted by lulian... –  user3535074 Jun 17 at 20:57
1  
"How can I decode the JSON object?" The dataType: 'json' setting tells jQuery to decode it for you. So, as long as the response is valid JSON, data should already be an Array. Regarding "but only the first record," you'll have to loop with mysql_fetch_array() to gather a list of all records. mysql_fetch_array add all rows? –  Jonathan Lonowski Jun 17 at 21:06
    
Thanks a lot Jonathan! After checking all the advice in the posted suggestion from lulian I can see that the issue seems to be exactly that, that the object passed from the php script only includes the first record. Checking your link... –  user3535074 Jun 17 at 21:20

1 Answer 1

The issue was the JSON object returned by the PHP script. Adjusted the PHP script as below and now I can access all returned rows and individual field names.

<?php

    $host = "localhost";
    $user = "root";
    $pass = "muertealregueton";

    $databaseName = "zombieSurvival";
    $tableName = "TBLusers";


    include 'DB.php';
    $con = mysql_connect($host,$user,$pass);
    $dbs = mysql_select_db($databaseName, $con);


    $result = mysql_query("SELECT * FROM $tableName");
    $array = array();
    while(($row = mysql_fetch_array($result))) {
        $array[] = $row;
    }
    echo json_encode($array);

?>

and the ajax code which calls an alert on one of the later records returned:

$.ajax({
       url: '/PHP/getUserMarkers.php',
       data: "",
       dataType: 'json',
       success: function(data){


       //now I can access each row and field
       //like here I access the 3rd field of the 15th record
       alert(data[15][2]);



       }
       });
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.