2017-08-24 113 views
0

自從現在一週以來,我一直被這個錯誤困住,我用Google搜索並嘗試了所有可能的解決方案,包括Stackoverflow上的解決方案。這是我的代碼:檢查XML輸出是否有效

<?php 
require("phpsqlsearch_dbinfo.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=mysqli_connect ("localhost", $username, $password); 
if (!$connection) { 
    die("Not connected : " . mysqli_error()); 
} 
// Set the active mySQL database 
$db_selected = mysqli_select_db($connection, $database); 
if (!$db_selected) { 
    die ("Can\'t use db : " . mysqli_error($connection)); 
} 
// Search the rows in the markers table 
$query = sprintf("SELECT id, 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 markers HAVING distance < '%s' ORDER BY distance LIMIT 0 , 
    20", 
    mysqli_real_escape_string($connection, $center_lat), 
    mysqli_real_escape_string($connection, $center_lng), 
    mysqli_real_escape_string($connection, $center_lat), 
    mysqli_real_escape_string($connection, $radius)); 
$result = mysqli_query($connection, $query); 
$result = mysqli_query($connection, $query); 
if (!$result) { 
    die("Invalid query: " . mysqli_error($connection)); 
} 
header("Content-type: text/xml"); 
// Iterate through the rows, adding XML nodes for each 
while ($row = @mysqli_fetch_assoc($result)){ 
    $node = $dom->createElement("marker"); 
    $newnode = $parnode->appendChild($node); 
    $newnode->setAttribute("id", $row['id']); 
    $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(); 
?> 

我的目標是輸出的XML瀏覽器,以確保它的工作,但我已經收到這些錯誤:

enter image description here 欲瞭解更多信息,請參閱:https://developers.google.com/maps/solutions/store-locator/clothing-store-locator 能有人幫助我?

+0

您應該正確縮進您的代碼。它可以幫助每個人閱讀你的代碼,這對你也有好處。 – ryantxr

+2

你可以粘貼你的XML輸出嗎?這是一個瀏覽器消息,而不是PHP error_log消息 – delboy1978uk

+0

@ delboy1978uk我的XML輸出是附加的圖像 – sankareb

回答

-1

這是瀏覽器消息,而不是PHP error_log消息。

某些東西在XML之前輸出。很可能是PHP警告或通知。抑制垃圾!

要獲得最佳錯誤記錄體驗,請將error_reporting設置爲-1,關閉display_errors,然後設置自定義error_log。然後在終端中輸入tail -f /path/to/error_log。您的通知,警告和錯誤現在將實時滾動瀏覽,而不會扭曲網頁的顯示。

+0

感謝您的努力!但我沒有使用cdl接口,我寧願使用xampp中的phpmyadmin並將其作爲編輯器升級! – sankareb

+0

我做了你所說的,但現在我得到了另一個錯誤: 未定義的索引:lat在C:\ xampp \ htdocs \ storelocator.php在第4行 未定義的索引:lng在C:\ xampp \ htdocs \ storelocator.php在第5行 未定義的索引:第6行的C:\ xampp \ htdocs \ storelocator.php中的半徑 – sankareb

+0

這意味着$ _GET [「lat」]未設置。你確定你不是指$ _POST?一個GET var在url中就像http://blah.com/my/url?getvar=value – delboy1978uk

0

沒有足夠的資源去實際解決您的問題。您可以使用以下內容來確保您獲得期望的輸入。

$required = ['lat', 'lng']; 
$missing = array_diff($required, $_GET); 
if (! empty($missing)) { 
    $missingString = implode(',', $missing); 
    die("missing {$missingString}"); 
} 

在XML中,開口

<?XML版本= 「1.0」 編碼= 「UTF-8」?>

之前它不能有一個空間。在回聲輸出XML之前,您的PHP代碼在某處輸出空格。

+0

我把它放在我的代碼的頭部,這是我在瀏覽器上輸出的結果: ** missing * * **緯度**,** lng ** 如果我刪除它,然後我得到: **注意:未定義的索引:在第5行C:\ xampp \ htdocs \ storelocator.php ** – sankareb

+0

$ _GET確實不包含你認爲它包含的內容。檢查您如何訪問該頁面。 – ryantxr

+0

!我盡我所能,但仍然沒有解決辦法!如果你能幫助我,請!這是我所遵循的教程:https://developers.google.com/maps/solutions/store-locator/clothing-store-locator – sankareb