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?
}
});
localhost
machine, posting your password is not advisable. – JakeGould Jun 17 at 20:54dataType: 'json'
setting tells jQuery to decode it for you. So, as long as the response is valid JSON,data
should already be anArray
. Regarding "but only the first record," you'll have to loop withmysql_fetch_array()
to gather a list of all records. mysql_fetch_array add all rows? – Jonathan Lonowski Jun 17 at 21:06