2016-04-23 39 views
-1

我有一個從MySQL生成XML似乎與郵件標題有關的問題header(「Content-type:text/xml」);This link提到這個問題,但在我的情況下並沒有解決問題。後指的是2007年GMAP例如,不the updated 2009 versionMySQL到XML郵件標題問題

這裏是全碼...

$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 ("99.99.99", "abcdefg", "zzzzzz"); 
if (!$connection) { 
    die("Not connected : " . mysql_error()); 
} 

// Set the active mySQL database 
$db_selected = mysql_select_db("xxxxxxx", $connection); 
if (!$db_selected) { 
    die ("Can\'t use db : " . mysql_error()); 
} 

// 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 ", 
    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()); 
} 
// - !!!! This is the problem line !!!!! 
//header("Content-type: text/xml"); 

// Iterate through the rows, adding XML nodes for each 
$i = 1; 
while ($row = mysql_fetch_assoc($result)){ 
    echo $i.$row['name']."<br>"; // names correctly displayed 
    $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']); 
    $i = $i + 1; 
} 

echo $dom->saveXML(); 
echo "done ".$i; // Script ends ok $i = 16 

一切似乎是工作如圖回聲聲明 - ,但是由於缺乏消息標題行,沒有xml被回顯到屏幕。

我在做什麼錯?

+0

檢查這個問題對PHP結束標記「?>」通過HTTP發送輸出時。 5月,這將有助於 – ahPo

+0

感謝user2340218 - 跟進你所說的讓我想知道棄用警告。 – PeterK900

+0

你基本上拋棄了一些代碼,並說「不起作用」。我們不能自己運行代碼,因爲我們沒有你的服務器和數據庫。你介意編輯這個問題並解釋爲什麼你的代碼不能滿足你的期望嗎? –

回答

-1

答案似乎是不要忽視對棄用代碼的警告 - 即使用mysqli_而不是mysql_!這是我懷疑XML被認爲格式不正確的原因。

這裏是代碼,不給錯誤..

$con=mysqli_connect("servername","user","pwd","database"); 
// Check connection 
if (mysqli_connect_errno()) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 

// 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($con, $center_lat), 
    mysqli_real_escape_string($con, $center_lng), 
    mysqli_real_escape_string($con, $center_lat), 
    mysqli_real_escape_string($con, $radius)); 
//$result = mysql_query($query); 
$result=mysqli_query($con,$query); 

if (!$result) { 
    die("Invalid query: " . mysqli_error($con)); 
} 

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("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();