2014-03-13 56 views
0

我試圖遵循本教程:谷歌地圖商店定位器PHP/MySQL的

https://developers.google.com/maps/articles/phpsqlsearch_v3

不幸的是,我不斷收到空白值在我的XML輸出:

<Competitors> 
    <Competitors Name="" Address="" lat="" lng="" Distance="0"/> 
    <Competitors Name="" Address="" lat="" lng="" Distance="0"/> 
    <Competitors Name="" Address="" lat="" lng="" Distance="0"/> 
    <Competitors Name="" Address="" lat="" lng="" Distance="0"/> 
    <Competitors Name="" Address="" lat="" lng="" Distance="0"/> 
    <Competitors Name="" Address="" lat="" lng="" Distance="0"/> 
    <Competitors Name="" Address="" lat="" lng="" Distance="0"/> 
    <Competitors Name="" Address="" lat="" lng="" Distance="0"/> 
    <Competitors Name="" Address="" lat="" lng="" Distance="0"/> 
    <Competitors Name="" Address="" lat="" lng="" Distance="0"/> 
</Competitors> 

我已經在我的數據庫中建立一個類似於教程中所示的表格:

Table Screenshot

,這裏是我的生成XML,按照教程代碼:

<?php 
$username="#######"; 
$password="#######"; 
$database="#######"; 

// 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("Competitors"); 
$parnode = $dom->appendChild($node); 

// Opens a connection to a mySQL server 
$connection=mysql_connect ("host", $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 Competitors 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 Competitors 
HAVING distance < '%s' 
ORDER BY distance LIMIT 0 , 20", 
    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("Competitors"); 
    $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(); 
?> 

我不知道,我犯了一個錯誤 - 任何幫助將不勝感激!

+1

看起來像你有一個大寫問題('lat'vs '緯度'等)在您的查詢。 – geocodezip

+0

從5.5開始不推薦使用mysql,你可以使用mysqli_ *自5.3開始我建議移動到那個,https://wiki.php.net/rfc/mysql_deprecation –

+0

你有沒有像這樣在你的URL中添加類似文檔所說的內容? lat = 37&lng = -122&radius = 25 – AdRock

回答

0

您需要更換這個

// Opens a connection to a mySQL server 
$connection=mysql_connect ('host', $username, $password); 
if (!$connection) { 
    die("Not connected : " . mysql_error()); 
} 

// Opens a connection to a mySQL server 
$connection=mysql_connect ('localhost', $username, $password); 
if (!$connection) { 
    die("Not connected : " . mysql_error()); 
} 

就嘗試過了,我得到XML

也只是用來localhost,而不是"localhost"的代碼示例使PHP認爲它是一個常量