I have this PHP which is fetching all rows of address column from the database and i an using json_encode()
function to convert it into string and JSON.parse
in javascript but i am not getting output as expected
<?
include('dbcon.php');
$result = mysql_query("SELECT address FROM markers");
while ($row = mysql_fetch_assoc($result)) {
$new_array[] = $row;
}
print_r($new_array);
$add_js = json_encode( $new_array );
print_r($add_js);
?>
with print_r($new_array);
i am getting two dimentional arrary and getting null
in zeroth location. any changes i make it keeps on giving null at zeroth location.
var address = [JSON.parse( '<?php echo $add_js ?>' )];
var address
should store array of addresses but it is giving output when i alert in the array.
[object Object],[object Object],[object Object],[object Object],[object Object]
i want to store array of addresses after fetching them from database
[object Object]
is OK for an object that gets stringified from an alert)? Are you sure that you need the brackets (array literal) around theJSON.parse
expression? Also, you should not need to use a string and explicit parse, you can simply echo the JSON as a javascript literal into the code. – Bergi Oct 26 '13 at 12:21var address = <?php echo json_encode($some_variable);?>;
? – Wrikken Oct 26 '13 at 12:26