2011-09-29 112 views
0

过去几天我一直在玩Google Maps API。由于缺乏PHP技能,我陷入了困境。下面运行的PHP代码后,返回的XML是:使用Google Maps API检查XML

<?xml version="1.0"?> 
<markers/> 

我使用的链接是:http://www.icliqz.com/Dev/phpsqlsearch_genxml_php.php?latitude=41.782321&longitude=-72.612031&radius=50

我与玩耍的代码是:http://code.google.com/apis/maps/articles/phpsqlsearch.html

我使用Dreamweaver来处理这个项目。如果有通过PHP代码进行调试的好方法,我会非常感激。我试着用Google搜索,但我不确定什么是最适合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(); 

?> 

谢谢!

回答

1

尝试从mysql shell手动运行您的查询以查看距离显示哪些值。好像你没有从数据库中获得任何结果。

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 
+0

你绝对正确,但我不确定Having子句不起作用的原因。我不确定%s代表什么。从我抬头看,它将任何数字/浮点数转换为一个字符串。 – JK0124

+0

如果你想要做字符串替换,如下所示:$ query = sprintf(“Select bla bla%s”,myvalueToReplace) –