2010-03-18 135 views
1

更新2:所以我從來沒有在螢火蟲中使用過調試功能,但我只是看了腳本部分,我明白了,現在我會試着解決這個問題。在谷歌地圖上使用PHP/MySQL

無法加載來源: http://localhost/googleMap/phpsqlajax_genxml.php

你好,

我跟着下面 http://code.google.com/apis/maps/articles/phpsqlajax_v3.html#outputxml

本教程中,我遇到了麻煩,近則結束了,我希望有人否則這裏有 得到了這個工作,並可以幫助我發現我的問題。只要有4個 步驟,本教程

  • 創建表
  • 填寫表
  • 1.4.3 XML與PHP
  • 創建地圖

我順利地完成了所有步驟,但是輸出的xml 未被我創建的谷歌地圖讀取。這些文件都在同一個 目錄中,我沒有更改 教程中的任何文件名。本教程有一個臺階,以測試PHP文件名爲 phpsqlajax_genxml.php是輸出XML和我測試成功 它,它是。

問題是,地圖不是呈現我在 數據庫中的項目,應將其轉換爲xml供地圖讀取。

任何幫助,或者指着我在正確的方向會更 讚賞。

UPDATE 1:我意識到我沒有任何代碼顯示在這裏,只有3個文件,所以我不確定哪個最好用。我有一個問題,但可能會幫助我的問題。

在教程的XML輸出部分,我問過

調用此PHP腳本從瀏覽器 ,以確保它產生有效的XML。 如果您懷疑與 問題連接到數據庫,你可以 找到,如果你刪除 文件中的 頭設置爲文本/ xml內容類型的行更容易調試, 爲通常會導致你的瀏覽器到 嘗試解析XML,並可能使其難以看到您的調試 消息 。

這是phpsqlajax_dbinfo。PHP文件

<?php 
$username="root"; 
$password="root"; 
$database="root-googleMap"; 
?> 

這是生成XML的代碼,是後曾作過一個XML文檔,我可以打開,或者是暫時的轉換。如果是的話,我如何做上面的步驟來測試,我真的不明白。

<?php 
require("phpsqlajax_dbinfo.php"); 

// Start XML file, create parent node 
$doc = domxml_new_doc("1.0"); 
$node = $doc->create_element("markers"); 
$parnode = $doc->append_child($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 * FROM markers WHERE 1"; 
$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->create_element("marker"); 
    $newnode = $parnode->append_child($node); 

    $newnode->set_attribute("name", $row['name']); 
    $newnode->set_attribute("address", $row['address']); 
    $newnode->set_attribute("lat", $row['lat']); 
    $newnode->set_attribute("lng", $row['lng']); 
    $newnode->set_attribute("type", $row['type']); 
} 

$xmlfile = $doc->dump_mem(); 
echo $xmlfile; 

?> 

回答

3

我假設(和希望)你不使用PHP 4和了,這是你的問題的根源,涉及到DOM處理XML的功能已經被替換爲DOM文檔類,有不同的方法名稱。

因此,我已經重構你的代碼是用PHP 5兼容:

<?php 
require("phpsqlajax_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 * FROM markers WHERE 1"; 
$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("name", $row['name']); 
    $newnode->set_attribute("address", $row['address']); 
    $newnode->set_attribute("lat", $row['lat']); 
    $newnode->set_attribute("lng", $row['lng']); 
    $newnode->set_attribute("type", $row['type']); 
} 

echo $doc->saveXML(); 

?> 
+0

感謝安德烈的代碼。有幾點需要指出,mysql_connect之後有一個空格,它也應該通過'localhost'。繼續在第37行,set_attribute應該是setAttribute。 這將運行。再次謝謝你。我正在像OP一樣學習Google Maps API,並通過Google發現了這一點。 :3他們應該真的更新原來的帖子! – r109 2013-03-21 01:39:09