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?


       }
       });
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
user3535074
  • 1,268
  • 8
  • 26
  • 48
  • 3
    Even if this is a `localhost` machine, posting your password is not advisable. – Giacomo1968 Jun 17 '14 at 20:54
  • You can see an answer in this duplicate: [Converting JSON Object into Javascript array](http://stackoverflow.com/questions/20881213/converting-json-object-into-javascript-array) – Iulian Miu Jun 17 '14 at 20:56
  • Thanks Jake - oversight on my part... Gonna take a looking at the duplicate posted by lulian... – user3535074 Jun 17 '14 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?](http://stackoverflow.com/questions/4643340/mysql-fetch-array-add-all-rows) – Jonathan Lonowski Jun 17 '14 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 '14 at 21:20
  • Was exactly that : great answer, thank you so much! – user3535074 Jun 17 '14 at 21:27

1 Answers1

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



       }
       });
user3535074
  • 1,268
  • 8
  • 26
  • 48