JavaScript通过php读取MySQL数据库

最近要需要利用百度API加载百度地图,百度API使用的是JavaScript,但是我需要利用数据库中的数据来控制
网上搜了很长时间,这方面资料没找到现成的,只能自己写了
开始我准备使用JDB的,搞了好长时间没有在我的服务器上加载成功,后来我想到MySQL的好基友PHP
于是我就自己写了下程序
其实原理很简单,我们在得到查询的数据库数据之后在加上一句

echo “var json=”.”’$json_out’;”;

然后在自己的HTML中引入mysql.php(数据库查询)

再通过json=eval(json);

接下来就是解析json数据了,当然你可以不使用json,其他的就直接echo JavaScript类型就可以了

网页http://www.nevergetme.com/map/news.html 可查看源代码

<!DOCTYPE html>
<html>
<head>
<metahttp-equiv="refresh"content="50">
<metahttp-equiv="Content-Type"content="text/html; charset=utf-8"/>
<metaname="viewport"content="initial-scale=1.0, user-scalable=no"/>
<styletype="text/css">
body, html,#allmap {width:100%;height:100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
#l-map{height:100%;width:78%;float:left;border-right:2px solid #bcbcbc;}
#r-result{height:100%;width:20%;float:left;}
</style>
<scripttype="text/javascript"src="http://api.map.baidu.com/api?v=2.0&ak=***********"></script>
<title>2015-2016</title>
</head>
<body>
<divid="allmap"></div>
</body>
</html>
<scripttype="text/javascript">
// 百度地图API功能
var map =newBMap.Map("allmap");
var point =newBMap.Point(116.396795,39.938395);
map.centerAndZoom(point,12);
map.enableScrollWheelZoom(true);
// 编写自定义函数,创建标注
function addMarker(point){
var marker =newBMap.Marker(point);
map.addOverlay(marker);
}
gohere:var here=1;
</script>
<scripttype="text/javascript"src="tooo.php"></script>
<scripttype="text/javascript">
json=eval(jstext);
for(var i =0; i < json.length; i ++){
var point =newBMap.Point(json[i].longitude,json[i].atitude);
var markerAll =newBMap.Marker(point);// 创建标注
map.addOverlay(markerAll);// 将标注添加到地图中
//addMarker(point);
var label =newBMap.Label(json[i].father+json[i].bianhao,{offset:newBMap.Size(20,-10)});
markerAll.setLabel(label);
}
</script>

 

posted @ 2017-06-07 09:19  hy战斗吧  阅读(1908)  评论(0编辑  收藏  举报