0

我已经设法使用 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

这个错误信息是什么意思?

4

1 回答 1

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>

您应该阅读过Developers.Google.com 教程

于 2013-04-18T23:29:51.300 回答