2

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

1
  • I can see the xml properly on your site. what is the the problem? Commented Apr 22, 2011 at 23:17

2 Answers 2

1

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>';
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Actually, I can see your XML, everything seem to be fine?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.