我正在学习PHP和MySQL。我试图教程:Google Maps API商店定位器问题
https://developers.google.com/maps/articles/phpsqlsearch_v3?hl=en
代码查询似乎回来了空。根据说明,我的数据库设置中有一张表。使用回声调用我已经确认,我得到一个有效的数据库连接,所有变量被正确调用。如果我在该表上运行基本的数据库查询(即没有任何数学或冲刺),我得到表中返回的行。根据教程(dbname,用户名和密码),“base2.php”文件只包含3个项目,这些都是正确的。所以,当我使用所需的URL语法从本地主机加载页面:
pagename.php?lat=37&lng=-122&radius=25
我只得到与XML头一个空白页。正如我所说,使用echo的,我已经确认脚本得到尽可能远的查询,它被返回为空。有没有人玩过本教程,并发现任何问题?我使用IE11,Chrome和FF获得相同的结果。我正在使用WAMPserver v2.4,PHP 5.4.12,MySQL 5.6.12和Apache 2.4.4。数据库中的表格具有正确的行和值,并具有数据。
<?php require "base2.php";
// Get parameters from URL
$center_lat = $_GET["lat"];
$center_lng = $_GET["lng"];
$radius = $_GET["radius"];
// 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", $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());
}
// Search the rows in the stores table
$query = sprintf("SELECT Name, Address, lat, lng, (3959 * acos(cos(radians('%s')) * cos( radians(lat)) * cos(radians(lng) - radians('%s')) + sin(radians('%s')) * sin(radians(lat)))) AS distance FROM stores HAVING distance < '%s' ORDER BY distance LIMIT 0 , 10",
mysql_real_escape_string($center_lat),
mysql_real_escape_string($center_lng),
mysql_real_escape_string($center_lat),
mysql_real_escape_string($radius));
$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)){
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("Name", $row['Name']);
$newnode->setAttribute("Address", $row['Address']);
$newnode->setAttribute("lat", $row['lat']);
$newnode->setAttribute("lng", $row['lng']);
$newnode->setAttribute("distance", $row['distance']);
}
echo $dom->saveXML();
?>
如果查询返回空,您是否尝试删除'HAVING'子句,因此它至少会显示它计算的距离? – Wrikken
嘿,谢谢你的回复。当我删除HAVING子句时,我确实得到一个结果,所有行都返回并包含distance属性,并创建XML。这是什么意思? – Longman
这意味着他们离你更远,那你显然认为......距离是什么意思?另外,如果您的意思是_numbers_,那么不要使用_strings_,'distance''%s''看起来很奇怪,应该是'distance <%d'或'distance <%f'。 – Wrikken