Trying to pass multidimensional php array to Javascript using JSON. The code refuses to enter the success state of JSON, why is this?
This is javascript file:
array = [];
function callback(arr)
{
console.log(array);
//simpleText.setText = array[0]
};
$(document).ready(function() {
$.getJSON('database.php', function(phpdata){
console.log("po");
console.log(phpdata);
callback(phpdata);
});
});
And php file:
header("Content-type: application/json");
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("atlas") or die(mysql_error());
$data = mysql_query("SELECT * FROM questions")
or die(mysql_error());
$i = 0;
$result_array = array();
while ($Row = mysql_fetch_array($data))
{
$user[] = array(
'id'=>$Row['id'],
'q'=>$Row['q'],
'a'=>$Row['a'],
'coordx'=>$Row['coordx'],
'coordy'=>$Row['coordy'],
);
}
var_dump($user);
$json = json_encode($user[0]);
echo $json;
$user[0], which is one row, which is a one-dimensional array.var_dump()out of the script. It will be read by$.getJSON, but it's not valid JSON.mysql_fetch_assocand then$user[] = $Row;, save yourself some time and energy by letting the mysql driver do the work for you (us2.php.net/mysql_fetch_assoc)