0

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?


       }
       });
5
  • You can see an answer in this duplicate: Converting JSON Object into Javascript array Commented Jun 17, 2014 at 20:56
  • Thanks Jake - oversight on my part... Gonna take a looking at the duplicate posted by lulian... Commented Jun 17, 2014 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? Commented Jun 17, 2014 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... Commented Jun 17, 2014 at 21:20
  • Was exactly that : great answer, thank you so much! Commented Jun 17, 2014 at 21:27

1 Answer 1

0

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]);



       }
       });

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.