【发布时间】:2011-09-29 11:45:02
【问题描述】:
过去几天我一直在使用 Google Maps API。由于缺乏 PHP 技能,我陷入了困境。运行下面的PHP代码后,返回的XML是:
<?xml version="1.0"?>
<markers/>
我正在玩的代码来自:http://code.google.com/apis/maps/articles/phpsqlsearch.html
我正在使用 Dreamweaver 来处理这个项目。如果有一种通过 PHP 代码进行调试的好方法,我将不胜感激。我试着用谷歌搜索它,但我不确定什么最适合 PHP 新手。 我检查了数据库中记录的半径。它们都在我正在测试的坐标的 50 英里范围内。
PHP代码是:
<?php
require("phpsqlsearch_dbinfo.php");
// Start XML file, create parent node
$doc = new DOMDocument("1.0");
$node = $doc->createElement("markers");
$parnode = $doc->appendChild($node);
// Opens a connection to a mySQL server
$connection=mysql_connect (localhost, $username, $password);
if (!$connection) {
die('Not connected : ' . mysql_error());
}
// Set the active mySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Select all the rows in the markers table
$query = "SELECT user, latitude, longitude, ( 3959 * acos( cos( radians('%s') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( latitude ) ) ) ) AS distance FROM tbl_UserLatLng HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20";
$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 = $doc->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->set_attribute("user", $row['user']);
$newnode->set_attribute("latitude", $row['latitude']);
$newnode->set_attribute("longitude", $row['longitude']);
$newnode->set_Attribute("distance", $row['distance']);
}
echo $doc->saveXML();
?>
提前致谢!
【问题讨论】:
标签: php mysql google-maps