php - Google Maps API 商店定位器问题

标签 php mysql sql

我正在学习 PHP 和 MySQL。我正在尝试教程:

https://developers.google.com/maps/articles/phpsqlsearch_v3?hl=en

代码中的查询似乎返回空。按照说明,我的数据库设置中有一个表。使用 echo 调用,我已经确认我正在获得有效的数据库连接,所有变量都被正确调用。如果我在该表上运行基本的数据库查询(即没有任何数学或冲刺),我会得到从表返回的行。 “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();
?>

最佳答案

您必须考虑这一行的国际 ASCII 字符 header("内容类型:text/xml"); 像这样 header("内容类型:text/xml charset=utf-8"); 然后就会生成xml文件

关于php - Google Maps API 商店定位器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22056678/

相关文章:

php - mysql SELECT distinct row from col1 WHERE col2=x

mysql - SQL查询结果返回集相同的id具有不同的值

mysql - 慢速三重内部连接

mysql - 有条件地显示 MySQL 存储过程中查询的一部分

php - MySQLi PHP -> 使用获取数组时 LEFT JOIN 返回 Null

sql - SQL 注入(inject)中的双连字符/破折号。它们是用来做什么的?

php - 将 bool 值 true/false 插入到 Postgres 和 MySQL

javascript - Laravel Dusk - 执行 javascript 脚本并等待 Promise 被解析以返回值

php - Jquery PHP mySQL ajax 方法将数据同步到两个客户端?

mysql - 在 MySql 中使用自连接到数据透视表