| Using PHP 5 /MySQL to output XML for Google Maps |
|
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 PHP5First, 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:
Using PHP's dom functions to output XMLCheck 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:
Checking that XML output worksCall 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. |
|||