Using PHP's DOM functions to Output XML

Hi,

I’m trying to dynamically output regions within a country from the database to create markers on a google map, and am following a tutorial and its exactly how they want me to do it but its not working for me.

I get an error and no xml output.

<?php
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);

// Select all the rows in the markers table
$query = "SELECT Nom_Rsrt, lat, lng, IdCntry_Rsrt FROM tbl_resorts WHERE (IdCntry_Rsrt=".$selectCountry.")";
$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 = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
//$newnode->setAttribute("name",$row['name']);
$newnode->setAttribute("address", $row['Nom_Rsrt']);
$newnode->setAttribute("lat", $row['lat']);
$newnode->setAttribute("lng", $row['lng']);
//$newnode->setAttribute("type", $row['type']);
}
echo $dom->saveXML();
?>

And this is the error I’m getting

Warning: DOMElement::setAttribute(): string is not in UTF-8 in \CSFFILES11\WEBSITES\dev\new_checksafetyfirst\en\result.php on line 125 Warning: DOMElement::setAttribute(): string is not in UTF-8 in \CSFFILES11\WEBSITES\dev\new_checksafetyfirst\en\result.php on line 125 Warning: DOMElement::setAttribute(): string is not in UTF-8 in \CSFFILES11\WEBSITES\dev\new_checksafetyfirst\en\result.php on line 125 Warning: DOMElement::setAttribute(): string is not in UTF-8 in \CSFFILES11\WEBSITES\dev\new_checksafetyfirst\en\result.php on line 125 Warning: DOMElement::setAttribute(): string is not in UTF-8 in \CSFFILES11\WEBSITES\dev\new_checksafetyfirst\en\result.php on line 125 Warning: DOMDocument::saveXML(): output conversion failed due to conv error, bytes 0xE1 0x64 0x69 0x7A in \CSFFILES11\WEBSITES\dev\new_checksafetyfirst\en\result.php on line 131

I’m not sure what going wrong as the code seems perfectly fine to me

OK I seem to have solved the UTF-8 issue with below

 $newnode->setAttribute("address",  utf8_encode($row['Nom_Rsrt']));
 $newnode->setAttribute("lat",  utf8_encode($row['lat']));
 $newnode->setAttribute("lng",  utf8_encode($row['lng']));

But still no output as I’m getting this error now

Warning: mysql_pconnect(): MySQL server has gone away in

Which relates to

$link=mysql_pconnect($host,$user,$pass);
mysql_select_db($db);

But I’m sure my select query is correct

Ok it seems the last error is unrelated and have cleared up th eothers, and change the code as above, but I’m still not getting any xml outputting to the browser

<!-- Start of Google Map API -->
<?php
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);

// Select all the rows in the markers table
$query = mysql_query("SELECT Nom_Rsrt, lat, lng, IdCntry_Rsrt FROM tbl_resorts WHERE (IdCntry_Rsrt='".$selectCountry."')");

header("Content-type: text/xml");

// Iterate through the rows, adding XML nodes for each

while ($row = mysql_fetch_assoc($query)){
// ADD TO XML DOCUMENT NODE
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("address", utf8_encode($row['Nom_Rsrt']));
$newnode->setAttribute("lat", utf8_encode($row['lat']));
$newnode->setAttribute("lng", utf8_encode($row['lng']));
}
echo $dom->saveXML();
?>

It does though seem to be gathering the info from the database correctly as I used var_dump($row) in the while loop and I got the following output which is correct

array(4) { ["Nom_Rsrt"]=> string(9) "Barcelona" ["lat"]=> string(9) "41.394768" ["lng"]=> string(8) "2.078728" ["IdCntry_Rsrt"]=> string(1) "1" } 

I just cant get the xml to out put so I can move on

have a look at the (generated) HTML source code in the browser.

Ah yes, thank you Dormilich

<?xml version="1.0"?>
<markers><marker address="Barcelona" lat="41.394768" lng="2.078728"/></markers>

That looks about right doesn’t it

Cheers

I’d say so.

Are you sure you want to use p(ersistent)connect here?

mysql_pconnect() acts very much like mysql_connect() with two major differences.

First, when connecting, the function would first try to find a (persistent) link that’s already open with the same host, username and password. If one is found, an identifier for it will be returned instead of opening a new connection.

Second, the connection to the SQL server will not be closed when the execution of the script ends. Instead, the link will remain open for future use (mysql_close() will not close links established by mysql_pconnect()).

i.e. persistent does not mean “please don’t go away on me” it means “stick around I’ll be using you again”

except that the code hasn’t stuck around as that interface is no longer supported at all in the current version of PHP.

???

Persistent connection support was introduced in PHP 5.3 for the mysqli extension. Support was already present in PDO MYSQL and ext/mysql.

I agree that spending any time on patching up old DEPRECATED mysql_ code is a waste of time.
But AFAIK (I’ve never used them) persistent connections are still supported.

I meant the mysql_ version no longer exists.

I don’t know why any persistent versions exist at all - there must be some extremely specialised use that needs them.

1 Like

Why have there been afew posts about those lately. I’ve never heard of that until now…

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.