1

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

9
  • Can you please post the results which your PHP generates? Commented Oct 26, 2013 at 12:18
  • First of all initialize the $new_array by adding $new_array = arary() at the begining of you code. Then alert won't display json string, it is an object, rather than using alert() for debuging, add console.log(address); and checkout your browser console (f11). Commented Oct 26, 2013 at 12:20
  • What's wrong with that array of objects, what did you expect ([object Object] is OK for an object that gets stringified from an alert)? Are you sure that you need the brackets (array literal) around the JSON.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. Commented Oct 26, 2013 at 12:21
  • 1
    results of php are ' Array ( [0] => Array ( [address] => Janak Puri, New Delhi, India ) [1] => Array ( [address] => Sector 63, Noida ) [2] => Array ( [address] => Dwarka, New Delhi, India ) [3] => Array ( [address] => Laxmi Nagar,new delhi ) [4] => Array ( [address] => Gurgaon, Haryana, India ) ) [{"address":"Janak Puri, New Delhi, India"},{"address":"Sector 63, Noida"},{"address":"Dwarka, New Delhi, India"},{"address":"Laxmi Nagar,new delhi"},{"address":"Gurgaon, Haryana, India"}] ' these are results fron temporary database. Commented Oct 26, 2013 at 12:22
  • Why are you not just using var address = <?php echo json_encode($some_variable);?>;? Commented Oct 26, 2013 at 12:26

2 Answers 2

2

So $new_array is an array of associative arrays, and it is decoded in JavaScript as an array of objects. If you want an array of strings instead, then you need to store strings in $new_array:

$new_array = array();
while ($row = mysql_fetch_assoc($result)) {
   $new_array[] = $row['address'];   // <-- This line
}

print_r($new_array);

$add_js = json_encode( $new_array );

print_r($add_js);
0
1

I think this is enough for your need:

var address = <?php echo $add_js ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.