Reputation: 165
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('<','<',$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 ($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
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
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