Download the Flash plugin.
Home arrow Google Maps arrow Using PHP 5 /MySQL to output XML for Google Maps
Using PHP 5 /MySQL to output XML for Google Maps Print E-mail
This tutorial is based on Pamela Fox’s excelent Php/ MySql tutorial but is intended for developers who are using PHP 5 and no longer have access to the Dom XML function.

The Dom XML funchtion was only considered experimental and was removed as of PHP 5.  You can attempt to uses PHP’s ehco function but if you search the Google Groups discussion board you will see that it creates all kinds of inconsistent browser problems and would not be the recommend method.

So if you need XML support with PHP 5 you have to use the DOM extension and the domxml extension used in Pamela’s tutorial is not compatible with the DOM extension.

The great thing about using DOM function is that there is no installation needed to use these functions; they are part of the PHP 5 core.

Outputting XML with PHP5

First, you should put your database connection information in a separate file. This is generally a good idea whenever you're using PHP to access a database, as it keeps your confidential information in a file that you won't be tempted to share. In the Maps API forum, we've occasionally had people accidentally publish their database connection information when they were just trying to debug their XML-outputting code. The file should look like this, but with your own database information filled in. phpsqlajax_dbinfo.php:

<?
$username="username";
$password="password";
$database="username-databaseName";
?>

Using PHP's dom functions to output XML

Check your configuration and make sure you are using PHP5.  In PHP, first initialize a new XML document and create the "markers" parent node. Then connect to the database, execute a SELECT * (select all) query on the markers table, and iterate through the results. For each row in the table (each location), create a new XML node with the row attributes as XML attributes, and append it to the parent node. Then dump the XML to the screen.

The PHP file that does all that is shown below:

<?php  
require("phpsqlajax_dbinfo.php"); 
// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node); 
// 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"); 
// 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['address']); 
$newnode->setAttribute("lat", $row['lat']); 
$newnode->setAttribute("lng", $row['lng']); 
$newnode->setAttribute("type", $row['type']);
echo $dom->saveXML();
?>

Checking that XML output works

Call this PHP script from the browser to make sure it's producing valid XML. If you suspect there's a problem with connecting to your database, you may find it easier to debug if you remove the line in the file that sets the header to the text/xml content type, as that usually causes your browser to try to parse XML and may make it difficult to see your debugging messages.

 

Google Map Plotter