user671891
user671891

Reputation: 165

Why am I not getting XML output from this php file?

I'm supposed to be getting XML output from the following php file that is accessing information from a database.

Here's the php file:

 <?php
 require("phpsqlajax_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 ($server, $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)){
   // 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>';

 ?>

Here's a link to the XML that I should be getting:

http://code.google.com/apis/earth/articles/phpsqlearth.html

Here's a link to the site that I'm not getting output from:

http://thehobbit2movie.com/phpsqlajax_genxml.php

Upvotes: 2

Views: 934

Answers (2)

James C
James C

Reputation: 14159

I think that it is returning fine, it just might not be visible in your browser unless you choose "view source" on the blank looking page.

Try to modifying the code just after the header() call so it looks like:

// Start XML file, echo parent node
echo '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
echo '<markers>';

Upvotes: 1

Laurent Bourgault-Roy
Laurent Bourgault-Roy

Reputation: 2814

Hum, couple of comment to improve your code

1- parseToXML could be replaced by the native htmlspecialchars function.

http://php.net/manual/en/function.htmlspecialchars.php

2- SELECT * FROM markers WHERE 1

No need for the WHERE 1, you just need SELECT * FROM markers. WHERE is not needed when you don't filter

OK, to answer your question, try removing the @ in from of mysql_fetch_assoc. You may see an error appearing. That is probably what is preventing your script from executing correctly.

Hope that help

Upvotes: 0

Related Questions