Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP生成XML时的奇怪行为_Php_Mysql_Xml - Fatal编程技术网

使用PHP生成XML时的奇怪行为

使用PHP生成XML时的奇怪行为,php,mysql,xml,Php,Mysql,Xml,我在php/mysql中遇到了一些非常奇怪的行为。我目前有一个包含名称、地址、lat、lng和类型值的位置数据库 我使用以下代码从数据库生成xml <?php require("dbinfo.php"); function parseToXML($htmlStr) { $xmlStr=str_replace('<','&lt;',$htmlStr); $xmlStr=str_replace('>','&gt;',$xmlStr);

我在php/mysql中遇到了一些非常奇怪的行为。我目前有一个包含名称、地址、lat、lng和类型值的位置数据库

我使用以下代码从数据库生成xml

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

function parseToXML($htmlStr) 
{ 
    $xmlStr=str_replace('<','&lt;',$htmlStr); 
    $xmlStr=str_replace('>','&gt;',$xmlStr); 
    $xmlStr=str_replace('"','&quot;',$xmlStr); 
    $xmlStr=str_replace("'",'&#39;',$xmlStr); 
    $xmlStr=str_replace("&",'&amp;',$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>';

?>
@mysql\u fetch\u assoc($result)禁止sql错误。您可能有一些被忽略的小问题。将其更改为mysql\u fetch\u assoc($result),然后查看发生了什么