问题描述
我正在使用 https:/中提供的PHP,MySQL和Google Maps示例创建商店定位器. /developers.google.com/maps/articles/phpsqlsearch_v3 .
我在编写XML文档时遇到问题.我可以从Querious(DB app)成功运行查询,但是当尝试运行所提供的脚本(我将其修改为mysqli)时,对我来说效果不佳.我什么也没得到.如示例中提供的那样设置数据库.这是我使用的修改后的PHP:
I am having issues getting the XML doc to write. I can run the query successful from Querious(DB app), but when attempting to run the provided script, which I modified to mysqli, things aren't working out for me. I am getting nothing returned. The DB is set up as supplied in the example. Here is the modified PHP that I use:
PHP
<?php
require_once('../includes/connection.php');
require_once('../includes/function.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);
// Search the rows in the markers table
$query = sprintf("SELECT address, name, lat, lng, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",
mysqli_real_escape_string($mysqli , $center_lat),
mysqli_real_escape_string($mysqli , $center_lng),
mysqli_real_escape_string($mysqli , $center_lat),
mysqli_real_escape_string($mysqli , $radius));
$result = mysqli_query($mysqli, $query);
$result = mysqli_query($mysqli, $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()
?>
我知道我的连接都成功,并且功能没有错误.感谢您对此的任何见识.
I know that both my connection are successful and that functions is error free. Thanks for any insight on this.
推荐答案
我不太确定无错误":)
I wouldn't be so sure about "error free" :)
您正在将mysql_fetch_assoc()
功能(不推荐使用的mysql_*
扩展名的一部分)与mysqli
一起使用,这两个功能不兼容.在此方法调用之前,@
隐藏了您通常会从错误地提取数据作为关联数组时看到的错误.
You are using the mysql_fetch_assoc()
function (part of the deprecated mysql_*
extensions) with mysqli
, which are not compatible. The @
before this method call is hiding the error that you would normally see from the incorrect fetch of the data as an associative array.
如果要使用mysqli
获得关联数组,则应使用:
If you want to get an associative array with mysqli
, you should use:
$array = $result->fetch_assoc();
http://php.net/manual/en/mysqli- result.fetch-assoc.php
http://us3.php.net/manual/zh/language.operators.errorcontrol.php
这篇关于Google Map API PHP示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!