1

befora I ask this i tried every solution on google and here on stackoverflow.

So in my js file I have this

    var locations = [
    ['Name','Country','City','lat','lng'],
    ['Name','Country','City','lat','lng'],
];

When i wrote this in file manually my mapp shown locations but I need to generate content of locations variable from mysql in php, is there something that I missing.I tried with ajax,console.log...etc

My PHP file

    $result = mysqli_query($link, "SELECT hospital_name,country,city,lat,lng FROM hospitals");
$to_encode = array();
while($row = mysqli_fetch_row($result)) {
  $to_encode[] = $row;
}
echo json_encode($to_encode);

I tried this but no success

$.getJSON( "process.php", function( json ) {
  var locations = json;
 });
8
  • Where's the attempted code? Commented Sep 22, 2015 at 1:36
  • 2
    Ajax will do what you need, have a look at api.jquery.com/jquery.getjson . It gets automatically parsed to a JS array Commented Sep 22, 2015 at 1:38
  • var location should be defined outside of the getJson function Commented Sep 22, 2015 at 1:42
  • I edited code still doesn't work, where do I wrong? Commented Sep 22, 2015 at 1:42
  • Try to console.log(json) Commented Sep 22, 2015 at 1:44

1 Answer 1

1

I just switched your mysqli_fetch_row to mysqli_fetch_array

    $result = mysqli_query($link, "SELECT hospital_name,country,city,lat,lng FROM hospitals");
    $to_encode = array();
    while($row = mysqli_fetch_array($result)) {
      $to_encode[] = $row;
    }
    echo json_encode($to_encode);
Sign up to request clarification or add additional context in comments.

1 Comment

Please add explanation not just code. It now becomes a game of find the difference whereas a simple explanation of what you changed goes a long way helping others see it and understand why. Your code solution might be great....but the answer is very low quality without people being able to review it quickly

Your Answer

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