在我问这个问题之前,我尝试了谷歌和 stackoverflow 上的所有解决方案。
所以在我的 js 文件中我有这个
var locations = [
['Name','Country','City','lat','lng'],
['Name','Country','City','lat','lng'],
];
当我手动在文件中写入此内容时,我的映射显示了位置,但我需要在 php 中从 mysql 生成位置变量的内容,是否有我遗漏的内容。我尝试使用 ajax、console.log...等
我的 PHP 文件
$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);
我尝试过,但没有成功
$.getJSON( "process.php", function( json ) {
var locations = json;
});
最佳答案
我刚刚将您的 mysqli_fetch_row
切换为 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);
关于javascript - PHP JSON 转换为 javascript 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32707037/