[英]Strange Behavior When Using PHP to Generate XML
我在php / mysql中遇到了一些非常奇怪的行為。 我目前有一個包含名稱,地址,lat,lng和類型值的位置數據庫。
我使用以下代碼從數據庫生成xml
<?php
require("dbinfo.php");
function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','<',$htmlStr);
$xmlStr=str_replace('>','>',$xmlStr);
$xmlStr=str_replace('"','"',$xmlStr);
$xmlStr=str_replace("'",''',$xmlStr);
$xmlStr=str_replace("&",'&',$xmlStr);
return $xmlStr;
}
// 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");
// Start XML file, echo parent node
echo '<markers>';
// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result))
{
if($row['lat'] != 0 && $row['lng'] !=0)
{
// ADD TO XML DOCUMENT NODE
echo '<marker ';
echo 'name="' . parseToXML($row['name']) . '" ';
echo 'address="' . parseToXML($row['address']) . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo 'type="' . $row['type'] . '" ';
echo '/>';
}
}
// End XML file
echo '</markers>';
?>
代碼似乎有效,但只有當它處於心情時。 它偶爾會生成適當的XML,但通常它什么都不做,返回一個空白頁面。 我從未見過這樣的行為,有誰知道我做錯了什么? 如果有任何幫助,我在MAMP本地運行它。 有誰知道發生了什么?
@mysql_fetch_assoc($ result)解決了sql錯誤。 您可能會遇到一些被忽略的輕微問題。 將它改為mysql_fetch_assoc($ result),看看發生了什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.