我已经设法使用PHP和XML输出单个表来绘制谷歌地图,但是我很难加入表格以获得相同的结果,这里是php代码:
<?php
// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
// Opens a connection to a MySQL server
$connection=mysql_connect ("localhost", "root", "z2f2w3k8") or die(mysql_error());
mysql_select_db("zena2") or die(mysql_error());
// Set the active MySQL database
$db_selected = mysql_select_db("zena2");
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Select all the rows in the markers table
$query = "SELECT customer.customerNo, customer.firstName, customer.lastName, customer.houseNum, customer.address, customer.telephone, map.customerNo, map.lat, map.long".
"FROM customer, map".
"WHERE (customer.customerNo = map.customerNo)";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Iterate through the rows, adding XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("name",$row['firstName'+'lastName']);
$newnode->setAttribute("address", $row['houseNum'+'address']);
$newnode->setAttribute("phone", $row['telephone']);
$newnode->setAttribute("lat", $row['lat']);
$newnode->setAttribute("lng", $row['long']);
$newnode->setAttribute("type", $row['customerNo']);
}
echo $dom->saveXML();
?>
这是HTML中的脚本
<script type="text/javascript" src="http://maps.googleapis.com/maps/api /js?sensor=false"></script>
<script type="text/javascript">
//<![CDATA[
var customIcons = {
1: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png',
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
}
};
function load() {
var map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(50.944693, -2.655044),
zoom: 13,
mapTypeId: 'roadmap'
});
var infoWindow = new google.maps.InfoWindow;
// Change this depending on the name of your PHP file
downloadUrl("xml.php", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute("name");
var address = markers[i].getAttribute("address");
var phone = markers[i].getAttribute("phone");
var type = markers[i].getAttribute("type");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var html = "<table> <tr> <th>" + name + "</th> </tr> <tr> <td>" + address + "</td> </tr> <tr> <td>" + phone + "</td> </tr>";
var icon = customIcons[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
icon: icon.icon,
shadow: icon.shadow
});
bindInfoWindow(marker, map, infoWindow, html);
}
});
}
function bindInfoWindow(marker, map, infoWindow, html) {
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
}
function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request, request.status);
}
};
request.open('GET', url, true);
request.send(null);
}
function doNothing() {}
//]]>
</script>
运行xml.php时出现的错误:
file is Invalid query: Unknown table 'customer' in field list
此错误消息的含义是什么?
答案 0 :(得分:1)
您正在不正确地查询SQL查询。在带有SQL查询的行上的最后一个引号之前添加一个空格。
所以你的代码应该是这样的:
$query = "SELECT customer.customerNo, customer.firstName, customer.lastName, customer.houseNum, customer.address, customer.telephone, map.customerNo, map.lat, map.long ".
"FROM customer, map ".
"WHERE (customer.customerNo = map.customerNo)";
此外,您可以删除此行,因为您选择了两次数据库:
mysql_select_db("zena2") or die(mysql_error());
这行格式错误:
<script type="text/javascript" src="http://maps.googleapis.com/maps/api /js?sensor=false"></script>