1

I want to take data from database and save it in an array. Like this

 var locations = [ ['Current', 18.53515053, 73.87944794, 2],

  ['VimanNagar', 18.5670762, 73.9084194, 1]
];

First of all I have created a php page

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "citytrans";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM driver_location";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
       echo json_encode($row);
    }
} else {
    echo "0 results";
}
$conn->close();
?>

which gives me below result

{"driver_id":"1","driver_lat":"18.53515053","driver_lng":"73.87944794","driver_code":"122"}{"driver_id":"2","driver_lat":"18.53640175","driver_lng":"73.88206482","driver_code":"133"}

Now I want to convert this into an array using Jquery (I want to decode it ), I just want drivers_lat and drivers_lng value from my jSON data fetched form the database show above.

I am using below code to parse the data form json

jQuery.ajax({                              
    url: baseurl +  "getdriverlocation.php",
    type: "JSON",
    async: false,
    success: function(data){
           var myArray = JSON.parse(data);
                console.log(myArray.driver_lat)
    } 
});

but it is giving me error (shown below)

SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 92 of the JSON data

I just want the two values from json data and save it in an array variable

Please help

11
  • the proplem is, that you are concatenating several json-objects to one string, which results in a non valid json. (in your while-loop) Commented Mar 31, 2016 at 12:41
  • echo json_encode($row); is in a while loop. So the string returned is not proper Commented Mar 31, 2016 at 12:41
  • There is no such type JSON, i think you missed dataType? Commented Mar 31, 2016 at 12:42
  • solution would be to first store all the result in one array, then json_encode that one array and echo that. Commented Mar 31, 2016 at 12:42
  • Use array..Push in array...and echo it by end of your php script... Commented Mar 31, 2016 at 12:42

5 Answers 5

2

Use this one..

jQuery.ajax({                              
url: baseurl +  "getdriverlocation.php",
type: "JSON",
async: false,
success: function(data){
       var myArray = jQuery.parseJSON(data);// instead of JSON.parse(data)
       jQuery(myArray).each(function( index, element ) {     
         console.log(element.driver_lat)
       });
} 
});
Sign up to request clarification or add additional context in comments.

5 Comments

Please provide me the complete output from getdriverlocation.php page
[{"driver_id":"2","driver_lat":"18.53640175","driver_lng":"73.88206482","driver_code":"133"}]
earlier i am gettinh this output {"driver_id":"1","driver_lat":"18.53515053","driver_lng":"73.87944794","driver_code":"122"}, {"driver_id":"2","driver_lat":"18.53640175","driver_lng":"73.88206482","driver_code":"133"} but after using your code i am getting [{"driver_id":"2","driver_lat":"18.53640175","driver_lng":"73.88206482","driver_‌​code":"133"}] only one
given in the question
It Works ! Thank you
1

In your php you should do :

if ($result->num_rows > 0) {
    // output data of each row <- no, build your data, then make only 1 output
    $output = array();
    while($row = $result->fetch_assoc()) {
       $output[] = $row;
    }
    echo json_encode($output);
}

Then in your jQuery, parse the whole json-decoded array...

2 Comments

what is the resulting json object your PHP sends to jQuery ?
[{"driver_id":"2","driver_lat":"18.53640175","driver_lng":"73.88206482","driver_code":"133"}]
1

Your json data is invalid. You must put comma bettween two JSON Objects Your respons must be {"driver_id":"1","driver_lat":"18.53515053","driver_lng":"73.87944794","driver_code":"122"}, {"driver_id":"2","driver_lat":"18.53640175","driver_lng":"73.88206482","driver_code":"133"}

1 Comment

this is also invalid
0

As i identified your Response JSON format is invalid, response JSON format should like this in order to parse into JSON via JSON.parse()

    [{"driver_id":"1","driver_lat":"18.53515053","driver_lng":"73.87944794","driver_code":"122"},
{"driver_id":"2","driver_lat":"18.53640175","driver_lng":"73.88206482","driver_code":"133"}]

Comments

0

Try this

$arrTmp = array();
if ($result->num_rows > 0) {
   // output data of each row
   while($row = $result->fetch_assoc()) {
      $arrTmp[] = $row;
   }
}
echo json_encode($arrTmp);

And maybe the jQuery tools bellow for old browsers

$.parseJSON(data);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.